Add strict typing to homekit locks (#73264)

This commit is contained in:
J. Nick Koston 2022-06-09 17:56:58 -10:00 committed by GitHub
parent 1dd7781acc
commit 5863d57e73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -114,6 +114,7 @@ homeassistant.components.homekit.aidmanager
homeassistant.components.homekit.config_flow homeassistant.components.homekit.config_flow
homeassistant.components.homekit.diagnostics homeassistant.components.homekit.diagnostics
homeassistant.components.homekit.logbook homeassistant.components.homekit.logbook
homeassistant.components.homekit.type_locks
homeassistant.components.homekit.type_triggers homeassistant.components.homekit.type_triggers
homeassistant.components.homekit.util homeassistant.components.homekit.util
homeassistant.components.homekit_controller homeassistant.components.homekit_controller

View File

@ -1,5 +1,6 @@
"""Class to hold all lock accessories.""" """Class to hold all lock accessories."""
import logging import logging
from typing import Any
from pyhap.const import CATEGORY_DOOR_LOCK from pyhap.const import CATEGORY_DOOR_LOCK
@ -12,7 +13,7 @@ from homeassistant.components.lock import (
STATE_UNLOCKING, STATE_UNLOCKING,
) )
from homeassistant.const import ATTR_CODE, ATTR_ENTITY_ID, STATE_UNKNOWN from homeassistant.const import ATTR_CODE, ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import callback from homeassistant.core import State, callback
from .accessories import TYPES, HomeAccessory from .accessories import TYPES, HomeAccessory
from .const import CHAR_LOCK_CURRENT_STATE, CHAR_LOCK_TARGET_STATE, SERV_LOCK from .const import CHAR_LOCK_CURRENT_STATE, CHAR_LOCK_TARGET_STATE, SERV_LOCK
@ -59,11 +60,12 @@ class Lock(HomeAccessory):
The lock entity must support: unlock and lock. The lock entity must support: unlock and lock.
""" """
def __init__(self, *args): def __init__(self, *args: Any) -> None:
"""Initialize a Lock accessory object.""" """Initialize a Lock accessory object."""
super().__init__(*args, category=CATEGORY_DOOR_LOCK) super().__init__(*args, category=CATEGORY_DOOR_LOCK)
self._code = self.config.get(ATTR_CODE) self._code = self.config.get(ATTR_CODE)
state = self.hass.states.get(self.entity_id) state = self.hass.states.get(self.entity_id)
assert state is not None
serv_lock_mechanism = self.add_preload_service(SERV_LOCK) serv_lock_mechanism = self.add_preload_service(SERV_LOCK)
self.char_current_state = serv_lock_mechanism.configure_char( self.char_current_state = serv_lock_mechanism.configure_char(
@ -76,7 +78,7 @@ class Lock(HomeAccessory):
) )
self.async_update_state(state) self.async_update_state(state)
def set_state(self, value): def set_state(self, value: int) -> None:
"""Set lock state to value if call came from HomeKit.""" """Set lock state to value if call came from HomeKit."""
_LOGGER.debug("%s: Set state to %d", self.entity_id, value) _LOGGER.debug("%s: Set state to %d", self.entity_id, value)
@ -89,7 +91,7 @@ class Lock(HomeAccessory):
self.async_call_service(DOMAIN, service, params) self.async_call_service(DOMAIN, service, params)
@callback @callback
def async_update_state(self, new_state): def async_update_state(self, new_state: State) -> None:
"""Update lock after state changed.""" """Update lock after state changed."""
hass_state = new_state.state hass_state = new_state.state
current_lock_state = HASS_TO_HOMEKIT_CURRENT.get( current_lock_state = HASS_TO_HOMEKIT_CURRENT.get(

View File

@ -1017,6 +1017,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = 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
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.homekit.type_triggers] [mypy-homeassistant.components.homekit.type_triggers]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true