mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Type hint additions (#26831)
* Type hint additions * Remove optional from sidebar_icon comment Co-Authored-By: Franck Nijhof <frenck@frenck.nl> * Remove optional from sidebar_title comment Co-Authored-By: Franck Nijhof <frenck@frenck.nl> * Fix issues after rebase and mypy 0.730
This commit is contained in:
@@ -4,6 +4,9 @@ from homeassistant.loader import bind_hass
|
||||
|
||||
from . import commands, connection, const, decorators, http, messages
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
DOMAIN = const.DOMAIN
|
||||
|
||||
DEPENDENCIES = ("http",)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import voluptuous as vol
|
||||
from voluptuous.humanize import humanize_error
|
||||
|
||||
from homeassistant.auth.models import RefreshToken, User
|
||||
from homeassistant.auth.providers import legacy_api_password
|
||||
from homeassistant.components.http.ban import process_wrong_login, process_success_login
|
||||
from homeassistant.const import __version__
|
||||
@@ -9,6 +10,9 @@ from homeassistant.const import __version__
|
||||
from .connection import ActiveConnection
|
||||
from .error import Disconnect
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
TYPE_AUTH = "auth"
|
||||
TYPE_AUTH_INVALID = "auth_invalid"
|
||||
TYPE_AUTH_OK = "auth_ok"
|
||||
@@ -87,7 +91,9 @@ class AuthPhase:
|
||||
await process_wrong_login(self._request)
|
||||
raise Disconnect
|
||||
|
||||
async def _async_finish_auth(self, user, refresh_token) -> ActiveConnection:
|
||||
async def _async_finish_auth(
|
||||
self, user: User, refresh_token: RefreshToken
|
||||
) -> ActiveConnection:
|
||||
"""Create an active connection."""
|
||||
self._logger.debug("Auth OK")
|
||||
await process_success_login(self._request)
|
||||
|
||||
@@ -12,6 +12,9 @@ from homeassistant.helpers.event import async_track_state_change
|
||||
from . import const, decorators, messages
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
|
||||
@callback
|
||||
def async_register_commands(hass, async_reg):
|
||||
"""Register commands."""
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""Connection session."""
|
||||
import asyncio
|
||||
from typing import Any, Callable, Dict, Hashable
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback, Context
|
||||
@@ -8,6 +10,9 @@ from homeassistant.exceptions import Unauthorized
|
||||
from . import const, messages
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
|
||||
class ActiveConnection:
|
||||
"""Handle an active websocket client connection."""
|
||||
|
||||
@@ -22,7 +27,7 @@ class ActiveConnection:
|
||||
else:
|
||||
self.refresh_token_id = None
|
||||
|
||||
self.subscriptions = {}
|
||||
self.subscriptions: Dict[Hashable, Callable[[], Any]] = {}
|
||||
self.last_id = 0
|
||||
|
||||
def context(self, msg):
|
||||
|
||||
@@ -8,6 +8,8 @@ from homeassistant.exceptions import Unauthorized
|
||||
from . import messages
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,9 @@ from .error import Disconnect
|
||||
from .messages import error_message
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
|
||||
class WebsocketAPIView(HomeAssistantView):
|
||||
"""View to serve a websockets endpoint."""
|
||||
|
||||
@@ -45,7 +48,7 @@ class WebSocketHandler:
|
||||
self.hass = hass
|
||||
self.request = request
|
||||
self.wsock = None
|
||||
self._to_write = asyncio.Queue(maxsize=MAX_PENDING_MSG)
|
||||
self._to_write: asyncio.Queue = asyncio.Queue(maxsize=MAX_PENDING_MSG)
|
||||
self._handle_task = None
|
||||
self._writer_task = None
|
||||
self._logger = logging.getLogger("{}.connection.{}".format(__name__, id(self)))
|
||||
@@ -106,7 +109,7 @@ class WebSocketHandler:
|
||||
# Py3.7+
|
||||
if hasattr(asyncio, "current_task"):
|
||||
# pylint: disable=no-member
|
||||
self._handle_task = asyncio.current_task()
|
||||
self._handle_task = asyncio.current_task() # type: ignore
|
||||
else:
|
||||
self._handle_task = asyncio.Task.current_task()
|
||||
|
||||
@@ -144,13 +147,13 @@ class WebSocketHandler:
|
||||
raise Disconnect
|
||||
|
||||
try:
|
||||
msg = msg.json()
|
||||
msg_data = msg.json()
|
||||
except ValueError:
|
||||
disconnect_warn = "Received invalid JSON."
|
||||
raise Disconnect
|
||||
|
||||
self._logger.debug("Received %s", msg)
|
||||
connection = await auth.async_handle(msg)
|
||||
self._logger.debug("Received %s", msg_data)
|
||||
connection = await auth.async_handle(msg_data)
|
||||
self.hass.data[DATA_CONNECTIONS] = (
|
||||
self.hass.data.get(DATA_CONNECTIONS, 0) + 1
|
||||
)
|
||||
@@ -170,13 +173,13 @@ class WebSocketHandler:
|
||||
break
|
||||
|
||||
try:
|
||||
msg = msg.json()
|
||||
msg_data = msg.json()
|
||||
except ValueError:
|
||||
disconnect_warn = "Received invalid JSON."
|
||||
break
|
||||
|
||||
self._logger.debug("Received %s", msg)
|
||||
connection.async_handle(msg)
|
||||
self._logger.debug("Received %s", msg_data)
|
||||
connection.async_handle(msg_data)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
self._logger.info("Connection closed by client")
|
||||
|
||||
@@ -7,6 +7,8 @@ from homeassistant.helpers import config_validation as cv
|
||||
from . import const
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs
|
||||
|
||||
# Minimal requirements of a message
|
||||
MINIMAL_MESSAGE_SCHEMA = vol.Schema(
|
||||
{vol.Required("id"): cv.positive_int, vol.Required("type"): cv.string},
|
||||
|
||||
@@ -10,6 +10,9 @@ from .const import (
|
||||
)
|
||||
|
||||
|
||||
# mypy: allow-untyped-calls, allow-untyped-defs
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the API streams platform."""
|
||||
entity = APICount()
|
||||
|
||||
Reference in New Issue
Block a user