From 5ed8de83484c88f38bfe3b27692500b992e3f0e8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 13 Oct 2023 14:45:59 -1000 Subject: [PATCH] Enable strict typing in HomeKit (#101968) --- .strict-typing | 10 +-- .../components/homekit/type_cameras.py | 7 +- .../components/homekit/type_covers.py | 13 +-- .../components/homekit/type_remotes.py | 8 +- mypy.ini | 82 +------------------ 5 files changed, 17 insertions(+), 103 deletions(-) diff --git a/.strict-typing b/.strict-typing index 4aa0a44c96d..2adadffea49 100644 --- a/.strict-typing +++ b/.strict-typing @@ -157,15 +157,7 @@ homeassistant.components.homeassistant_green.* homeassistant.components.homeassistant_hardware.* homeassistant.components.homeassistant_sky_connect.* homeassistant.components.homeassistant_yellow.* -homeassistant.components.homekit -homeassistant.components.homekit.accessories -homeassistant.components.homekit.aidmanager -homeassistant.components.homekit.config_flow -homeassistant.components.homekit.diagnostics -homeassistant.components.homekit.logbook -homeassistant.components.homekit.type_locks -homeassistant.components.homekit.type_triggers -homeassistant.components.homekit.util +homeassistant.components.homekit.* homeassistant.components.homekit_controller homeassistant.components.homekit_controller.alarm_control_panel homeassistant.components.homekit_controller.button diff --git a/homeassistant/components/homekit/type_cameras.py b/homeassistant/components/homekit/type_cameras.py index 6bc8e785c7f..ed26265be24 100644 --- a/homeassistant/components/homekit/type_cameras.py +++ b/homeassistant/components/homekit/type_cameras.py @@ -139,7 +139,7 @@ CONFIG_DEFAULTS = { @TYPES.register("Camera") -class Camera(HomeAccessory, PyhapCamera): +class Camera(HomeAccessory, PyhapCamera): # type: ignore[misc] """Generate a Camera accessory.""" def __init__( @@ -337,7 +337,8 @@ class Camera(HomeAccessory, PyhapCamera): async def _async_get_stream_source(self) -> str | None: """Find the camera stream source url.""" - if stream_source := self.config.get(CONF_STREAM_SOURCE): + stream_source: str | None = self.config.get(CONF_STREAM_SOURCE) + if stream_source: return stream_source try: stream_source = await camera.async_get_stream_source( @@ -419,7 +420,7 @@ class Camera(HomeAccessory, PyhapCamera): stderr_reader = await stream.get_reader(source=FFMPEG_STDERR) - async def watch_session(_): + async def watch_session(_: Any) -> None: await self._async_ffmpeg_watch(session_info["id"]) session_info[FFMPEG_LOGGER] = asyncio.create_task( diff --git a/homeassistant/components/homekit/type_covers.py b/homeassistant/components/homekit/type_covers.py index c8599b99664..1d60d405502 100644 --- a/homeassistant/components/homekit/type_covers.py +++ b/homeassistant/components/homekit/type_covers.py @@ -104,6 +104,7 @@ class GarageDoorOpener(HomeAccessory): """Initialize a GarageDoorOpener accessory object.""" super().__init__(*args, category=CATEGORY_GARAGE_DOOR_OPENER) state = self.hass.states.get(self.entity_id) + assert state serv_garage_door = self.add_preload_service(SERV_GARAGE_DOOR_OPENER) self.char_current_state = serv_garage_door.configure_char( @@ -124,7 +125,7 @@ class GarageDoorOpener(HomeAccessory): self.async_update_state(state) - async def run(self): + async def run(self) -> None: """Handle accessory driver started event. Run inside the Home Assistant event loop. @@ -165,7 +166,7 @@ class GarageDoorOpener(HomeAccessory): detected, ) - def set_state(self, value): + def set_state(self, value: int) -> None: """Change garage state if call came from HomeKit.""" _LOGGER.debug("%s: Set state to %d", self.entity_id, value) @@ -180,7 +181,7 @@ class GarageDoorOpener(HomeAccessory): self.async_call_service(DOMAIN, SERVICE_CLOSE_COVER, params) @callback - def async_update_state(self, new_state): + def async_update_state(self, new_state: State) -> None: """Update cover state after state changed.""" hass_state = new_state.state target_door_state = DOOR_TARGET_HASS_TO_HK.get(hass_state) @@ -235,7 +236,7 @@ class OpeningDeviceBase(HomeAccessory): CHAR_CURRENT_TILT_ANGLE, value=0 ) - def set_stop(self, value): + def set_stop(self, value: int) -> None: """Stop the cover motion from HomeKit.""" if value != 1: return @@ -243,7 +244,7 @@ class OpeningDeviceBase(HomeAccessory): DOMAIN, SERVICE_STOP_COVER, {ATTR_ENTITY_ID: self.entity_id} ) - def set_tilt(self, value): + def set_tilt(self, value: float) -> None: """Set tilt to value if call came from HomeKit.""" _LOGGER.info("%s: Set tilt to %d", self.entity_id, value) @@ -256,7 +257,7 @@ class OpeningDeviceBase(HomeAccessory): self.async_call_service(DOMAIN, SERVICE_SET_COVER_TILT_POSITION, params, value) @callback - def async_update_state(self, new_state): + def async_update_state(self, new_state: State) -> None: """Update cover position and tilt after state changed.""" # update tilt if not self._supports_tilt: diff --git a/homeassistant/components/homekit/type_remotes.py b/homeassistant/components/homekit/type_remotes.py index 0f6e22abe1d..e03b14f943a 100644 --- a/homeassistant/components/homekit/type_remotes.py +++ b/homeassistant/components/homekit/type_remotes.py @@ -165,19 +165,19 @@ class RemoteInputSelectAccessory(HomeAccessory, ABC): return list(self._get_mapped_sources(state)) @abstractmethod - def set_on_off(self, value): + def set_on_off(self, value: bool) -> None: """Move switch state to value if call came from HomeKit.""" @abstractmethod - def set_input_source(self, value): + def set_input_source(self, value: int) -> None: """Send input set value if call came from HomeKit.""" @abstractmethod - def set_remote_key(self, value): + def set_remote_key(self, value: int) -> None: """Send remote key value if call came from HomeKit.""" @callback - def _async_update_input_state(self, hk_state, new_state): + def _async_update_input_state(self, hk_state: int, new_state: State) -> None: """Update input state after state changed.""" # Set active input if not self.support_select_source or not self.sources: diff --git a/mypy.ini b/mypy.ini index 40d57a6b430..93fe5326e98 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1331,87 +1331,7 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true -[mypy-homeassistant.components.homekit] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.accessories] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.aidmanager] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.config_flow] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.diagnostics] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.logbook] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.type_locks] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.type_triggers] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - -[mypy-homeassistant.components.homekit.util] +[mypy-homeassistant.components.homekit.*] check_untyped_defs = true disallow_incomplete_defs = true disallow_subclassing_any = true