Add lock typing in components (#73539)

* Add lock typing in components

* Revert freedompro amends
This commit is contained in:
epenet 2022-06-15 15:23:36 +02:00 committed by GitHub
parent 8c0ae545c9
commit f8f1bfde21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 78 additions and 54 deletions

View File

@ -1,5 +1,6 @@
"""Support for August lock.""" """Support for August lock."""
import logging import logging
from typing import Any
from aiohttp import ClientResponseError from aiohttp import ClientResponseError
from yalexs.activity import SOURCE_PUBNUB, ActivityType from yalexs.activity import SOURCE_PUBNUB, ActivityType
@ -44,14 +45,14 @@ class AugustLock(AugustEntityMixin, RestoreEntity, LockEntity):
self._attr_unique_id = f"{self._device_id:s}_lock" self._attr_unique_id = f"{self._device_id:s}_lock"
self._update_from_data() self._update_from_data()
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
if self._data.activity_stream.pubnub.connected: if self._data.activity_stream.pubnub.connected:
await self._data.async_lock_async(self._device_id, self._hyper_bridge) await self._data.async_lock_async(self._device_id, self._hyper_bridge)
return return
await self._call_lock_operation(self._data.async_lock) await self._call_lock_operation(self._data.async_lock)
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
if self._data.activity_stream.pubnub.connected: if self._data.activity_stream.pubnub.connected:
await self._data.async_unlock_async(self._device_id, self._hyper_bridge) await self._data.async_unlock_async(self._device_id, self._hyper_bridge)

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from typing import Any
from homeassistant.components.lock import LockEntity, LockEntityFeature from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -66,26 +67,26 @@ class DemoLock(LockEntity):
self._jam_on_operation = jam_on_operation self._jam_on_operation = jam_on_operation
@property @property
def is_locking(self): def is_locking(self) -> bool:
"""Return true if lock is locking.""" """Return true if lock is locking."""
return self._state == STATE_LOCKING return self._state == STATE_LOCKING
@property @property
def is_unlocking(self): def is_unlocking(self) -> bool:
"""Return true if lock is unlocking.""" """Return true if lock is unlocking."""
return self._state == STATE_UNLOCKING return self._state == STATE_UNLOCKING
@property @property
def is_jammed(self): def is_jammed(self) -> bool:
"""Return true if lock is jammed.""" """Return true if lock is jammed."""
return self._state == STATE_JAMMED return self._state == STATE_JAMMED
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._state == STATE_LOCKED return self._state == STATE_LOCKED
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
self._state = STATE_LOCKING self._state = STATE_LOCKING
self.async_write_ha_state() self.async_write_ha_state()
@ -96,7 +97,7 @@ class DemoLock(LockEntity):
self._state = STATE_LOCKED self._state = STATE_LOCKED
self.async_write_ha_state() self.async_write_ha_state()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
self._state = STATE_UNLOCKING self._state = STATE_UNLOCKING
self.async_write_ha_state() self.async_write_ha_state()
@ -104,7 +105,7 @@ class DemoLock(LockEntity):
self._state = STATE_UNLOCKED self._state = STATE_UNLOCKED
self.async_write_ha_state() self.async_write_ha_state()
async def async_open(self, **kwargs): async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
self._state = STATE_UNLOCKED self._state = STATE_UNLOCKED
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -1,6 +1,8 @@
"""Support for Fibaro locks.""" """Support for Fibaro locks."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from homeassistant.components.lock import ENTITY_ID_FORMAT, LockEntity from homeassistant.components.lock import ENTITY_ID_FORMAT, LockEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
@ -37,18 +39,18 @@ class FibaroLock(FibaroDevice, LockEntity):
super().__init__(fibaro_device) super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id) self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
self.action("secure") self.action("secure")
self._state = True self._state = True
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
self.action("unsecure") self.action("unsecure")
self._state = False self._state = False
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if device is locked.""" """Return true if device is locked."""
return self._state return self._state

View File

@ -1,6 +1,8 @@
"""Support for Homematic locks.""" """Support for Homematic locks."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from homeassistant.components.lock import LockEntity, LockEntityFeature from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -33,19 +35,19 @@ class HMLock(HMDevice, LockEntity):
_attr_supported_features = LockEntityFeature.OPEN _attr_supported_features = LockEntityFeature.OPEN
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if the lock is locked.""" """Return true if the lock is locked."""
return not bool(self._hm_get_state()) return not bool(self._hm_get_state())
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Lock the lock.""" """Lock the lock."""
self._hmdevice.lock() self._hmdevice.lock()
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the lock.""" """Unlock the lock."""
self._hmdevice.unlock() self._hmdevice.unlock()
def open(self, **kwargs): def open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
self._hmdevice.open() self._hmdevice.open()

View File

@ -1,6 +1,8 @@
"""Support for KEBA charging station switch.""" """Support for KEBA charging station switch."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from homeassistant.components.lock import LockEntity from homeassistant.components.lock import LockEntity
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -51,15 +53,15 @@ class KebaLock(LockEntity):
return f"{self._keba.device_name} {self._name}" return f"{self._keba.device_name} {self._name}"
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._state return self._state
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock wallbox.""" """Lock wallbox."""
await self._keba.async_stop() await self._keba.async_stop()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock wallbox.""" """Unlock wallbox."""
await self._keba.async_start() await self._keba.async_start()

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
from kiwiki import KiwiClient, KiwiException from kiwiki import KiwiClient, KiwiException
import voluptuous as vol import voluptuous as vol
@ -89,7 +90,7 @@ class KiwiLock(LockEntity):
return name or specifier return name or specifier
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._state == STATE_LOCKED return self._state == STATE_LOCKED
@ -104,7 +105,7 @@ class KiwiLock(LockEntity):
self._state = STATE_LOCKED self._state = STATE_LOCKED
self.async_write_ha_state() self.async_write_ha_state()
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
try: try:

View File

@ -1,4 +1,8 @@
"""Platform for Mazda lock integration.""" """Platform for Mazda lock integration."""
from __future__ import annotations
from typing import Any
from homeassistant.components.lock import LockEntity from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -36,17 +40,17 @@ class MazdaLock(MazdaEntity, LockEntity):
self._attr_unique_id = self.vin self._attr_unique_id = self.vin
@property @property
def is_locked(self): def is_locked(self) -> bool | None:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self.client.get_assumed_lock_state(self.vehicle_id) return self.client.get_assumed_lock_state(self.vehicle_id)
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the vehicle doors.""" """Lock the vehicle doors."""
await self.client.lock_doors(self.vehicle_id) await self.client.lock_doors(self.vehicle_id)
self.async_write_ha_state() self.async_write_ha_state()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the vehicle doors.""" """Unlock the vehicle doors."""
await self.client.unlock_doors(self.vehicle_id) await self.client.unlock_doors(self.vehicle_id)

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import functools import functools
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -183,7 +184,7 @@ class MqttLock(MqttEntity, LockEntity):
await subscription.async_subscribe_topics(self.hass, self._sub_state) await subscription.async_subscribe_topics(self.hass, self._sub_state)
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._state return self._state
@ -197,7 +198,7 @@ class MqttLock(MqttEntity, LockEntity):
"""Flag supported features.""" """Flag supported features."""
return LockEntityFeature.OPEN if CONF_PAYLOAD_OPEN in self._config else 0 return LockEntityFeature.OPEN if CONF_PAYLOAD_OPEN in self._config else 0
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device. """Lock the device.
This method is a coroutine. This method is a coroutine.
@ -214,7 +215,7 @@ class MqttLock(MqttEntity, LockEntity):
self._state = True self._state = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device. """Unlock the device.
This method is a coroutine. This method is a coroutine.
@ -231,7 +232,7 @@ class MqttLock(MqttEntity, LockEntity):
self._state = False self._state = False
self.async_write_ha_state() self.async_write_ha_state()
async def async_open(self, **kwargs): async def async_open(self, **kwargs: Any) -> None:
"""Open the door latch. """Open the door latch.
This method is a coroutine. This method is a coroutine.

View File

@ -1,5 +1,6 @@
"""Nuki.io lock platform.""" """Nuki.io lock platform."""
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any
from pynuki.constants import MODE_OPENER_CONTINUOUS from pynuki.constants import MODE_OPENER_CONTINUOUS
import voluptuous as vol import voluptuous as vol
@ -92,15 +93,15 @@ class NukiDeviceEntity(NukiEntity, LockEntity, ABC):
return super().available and self._nuki_device.state not in ERROR_STATES return super().available and self._nuki_device.state not in ERROR_STATES
@abstractmethod @abstractmethod
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
@abstractmethod @abstractmethod
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
@abstractmethod @abstractmethod
def open(self, **kwargs): def open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
@ -112,15 +113,15 @@ class NukiLockEntity(NukiDeviceEntity):
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._nuki_device.is_locked return self._nuki_device.is_locked
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
self._nuki_device.lock() self._nuki_device.lock()
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
self._nuki_device.unlock() self._nuki_device.unlock()
def open(self, **kwargs): def open(self, **kwargs: Any) -> None:
"""Open the door latch.""" """Open the door latch."""
self._nuki_device.unlatch() self._nuki_device.unlatch()
@ -144,15 +145,15 @@ class NukiOpenerEntity(NukiDeviceEntity):
or self._nuki_device.mode == MODE_OPENER_CONTINUOUS or self._nuki_device.mode == MODE_OPENER_CONTINUOUS
) )
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Disable ring-to-open.""" """Disable ring-to-open."""
self._nuki_device.deactivate_rto() self._nuki_device.deactivate_rto()
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Enable ring-to-open.""" """Enable ring-to-open."""
self._nuki_device.activate_rto() self._nuki_device.activate_rto()
def open(self, **kwargs): def open(self, **kwargs: Any) -> None:
"""Buzz open the door.""" """Buzz open the door."""
self._nuki_device.electric_strike_actuation() self._nuki_device.electric_strike_actuation()

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Sequence from collections.abc import Sequence
from typing import Any
from pysmartthings import Attribute, Capability from pysmartthings import Attribute, Capability
@ -50,18 +51,18 @@ def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
class SmartThingsLock(SmartThingsEntity, LockEntity): class SmartThingsLock(SmartThingsEntity, LockEntity):
"""Define a SmartThings lock.""" """Define a SmartThings lock."""
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
await self._device.lock(set_status=True) await self._device.lock(set_status=True)
self.async_write_ha_state() self.async_write_ha_state()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
await self._device.unlock(set_status=True) await self._device.unlock(set_status=True)
self.async_write_ha_state() self.async_write_ha_state()
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._device.status.lock == ST_STATE_LOCKED return self._device.status.lock == ST_STATE_LOCKED

View File

@ -1,4 +1,6 @@
"""Support for StarLine lock.""" """Support for StarLine lock."""
from typing import Any
from homeassistant.components.lock import LockEntity from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -68,10 +70,10 @@ class StarlineLock(StarlineEntity, LockEntity):
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._device.car_state.get("arm") return self._device.car_state.get("arm")
def lock(self, **kwargs): def lock(self, **kwargs: Any) -> None:
"""Lock the car.""" """Lock the car."""
self._account.api.set_car_state(self._device.device_id, "arm", True) self._account.api.set_car_state(self._device.device_id, "arm", True)
def unlock(self, **kwargs): def unlock(self, **kwargs: Any) -> None:
"""Unlock the car.""" """Unlock the car."""
self._account.api.set_car_state(self._device.device_id, "arm", False) self._account.api.set_car_state(self._device.device_id, "arm", False)

View File

@ -1,5 +1,6 @@
"""Support for Subaru door locks.""" """Support for Subaru door locks."""
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -68,7 +69,7 @@ class SubaruLock(LockEntity):
self._attr_unique_id = f"{vin}_door_locks" self._attr_unique_id = f"{vin}_door_locks"
self._attr_device_info = get_device_info(vehicle_info) self._attr_device_info = get_device_info(vehicle_info)
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Send the lock command.""" """Send the lock command."""
_LOGGER.debug("Locking doors for: %s", self.car_name) _LOGGER.debug("Locking doors for: %s", self.car_name)
await async_call_remote_service( await async_call_remote_service(
@ -77,7 +78,7 @@ class SubaruLock(LockEntity):
self.vehicle_info, self.vehicle_info,
) )
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Send the unlock command.""" """Send the unlock command."""
_LOGGER.debug("Unlocking doors for: %s", self.car_name) _LOGGER.debug("Unlocking doors for: %s", self.car_name)
await async_call_remote_service( await async_call_remote_service(

View File

@ -1,6 +1,8 @@
"""Support for locks which integrates with other components.""" """Support for locks which integrates with other components."""
from __future__ import annotations from __future__ import annotations
from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.components.lock import ( from homeassistant.components.lock import (
@ -95,22 +97,22 @@ class TemplateLock(TemplateEntity, LockEntity):
return self._optimistic return self._optimistic
@property @property
def is_locked(self): def is_locked(self) -> bool:
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self._state in ("true", STATE_ON, STATE_LOCKED) return self._state in ("true", STATE_ON, STATE_LOCKED)
@property @property
def is_jammed(self): def is_jammed(self) -> bool:
"""Return true if lock is jammed.""" """Return true if lock is jammed."""
return self._state == STATE_JAMMED return self._state == STATE_JAMMED
@property @property
def is_unlocking(self): def is_unlocking(self) -> bool:
"""Return true if lock is unlocking.""" """Return true if lock is unlocking."""
return self._state == STATE_UNLOCKING return self._state == STATE_UNLOCKING
@property @property
def is_locking(self): def is_locking(self) -> bool:
"""Return true if lock is locking.""" """Return true if lock is locking."""
return self._state == STATE_LOCKING return self._state == STATE_LOCKING
@ -138,14 +140,14 @@ class TemplateLock(TemplateEntity, LockEntity):
) )
await super().async_added_to_hass() await super().async_added_to_hass()
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device.""" """Lock the device."""
if self._optimistic: if self._optimistic:
self._state = True self._state = True
self.async_write_ha_state() self.async_write_ha_state()
await self.async_run_script(self._command_lock, context=self._context) await self.async_run_script(self._command_lock, context=self._context)
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the device.""" """Unlock the device."""
if self._optimistic: if self._optimistic:
self._state = False self._state = False

View File

@ -1,6 +1,8 @@
"""Support for Volvo On Call locks.""" """Support for Volvo On Call locks."""
from __future__ import annotations from __future__ import annotations
from typing import Any
from homeassistant.components.lock import LockEntity from homeassistant.components.lock import LockEntity
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -30,10 +32,10 @@ class VolvoLock(VolvoEntity, LockEntity):
"""Return true if lock is locked.""" """Return true if lock is locked."""
return self.instrument.is_locked return self.instrument.is_locked
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the car.""" """Lock the car."""
await self.instrument.lock() await self.instrument.lock()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the car.""" """Unlock the car."""
await self.instrument.unlock() await self.instrument.unlock()

View File

@ -1,5 +1,6 @@
"""Locks on Zigbee Home Automation networks.""" """Locks on Zigbee Home Automation networks."""
import functools import functools
from typing import Any
import voluptuous as vol import voluptuous as vol
from zigpy.zcl.foundation import Status from zigpy.zcl.foundation import Status
@ -119,7 +120,7 @@ class ZhaDoorLock(ZhaEntity, LockEntity):
"""Return state attributes.""" """Return state attributes."""
return self.state_attributes return self.state_attributes
async def async_lock(self, **kwargs): async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock.""" """Lock the lock."""
result = await self._doorlock_channel.lock_door() result = await self._doorlock_channel.lock_door()
if isinstance(result, Exception) or result[0] is not Status.SUCCESS: if isinstance(result, Exception) or result[0] is not Status.SUCCESS:
@ -127,7 +128,7 @@ class ZhaDoorLock(ZhaEntity, LockEntity):
return return
self.async_write_ha_state() self.async_write_ha_state()
async def async_unlock(self, **kwargs): async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock.""" """Unlock the lock."""
result = await self._doorlock_channel.unlock_door() result = await self._doorlock_channel.unlock_door()
if isinstance(result, Exception) or result[0] is not Status.SUCCESS: if isinstance(result, Exception) or result[0] is not Status.SUCCESS: