Strictly type tradfri cover.py (#56390)

* Strictly type cover.py.

* Review comments from other PR.

* Update homeassistant/components/tradfri/cover.py

Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com>

* Update homeassistant/components/tradfri/cover.py

Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com>

* Update homeassistant/components/tradfri/cover.py

Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com>

* Update homeassistant/components/tradfri/cover.py

Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com>
This commit is contained in:
jan iversen 2021-09-21 13:15:45 +02:00 committed by GitHub
parent 7698c179ac
commit 518c99c8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,24 @@
"""Support for IKEA Tradfri covers.""" """Support for IKEA Tradfri covers."""
from __future__ import annotations
from typing import Any, Callable, cast
from pytradfri.command import Command
from homeassistant.components.cover import ATTR_POSITION, CoverEntity from homeassistant.components.cover import ATTR_POSITION, CoverEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .base_class import TradfriBaseDevice from .base_class import TradfriBaseDevice
from .const import ATTR_MODEL, CONF_GATEWAY_ID, DEVICES, DOMAIN, KEY_API from .const import ATTR_MODEL, CONF_GATEWAY_ID, DEVICES, DOMAIN, KEY_API
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Load Tradfri covers based on a config entry.""" """Load Tradfri covers based on a config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID] gateway_id = config_entry.data[CONF_GATEWAY_ID]
tradfri_data = hass.data[DOMAIN][config_entry.entry_id] tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
@ -21,7 +33,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TradfriCover(TradfriBaseDevice, CoverEntity): class TradfriCover(TradfriBaseDevice, CoverEntity):
"""The platform class required by Home Assistant.""" """The platform class required by Home Assistant."""
def __init__(self, device, api, gateway_id): def __init__(
self,
device: Command,
api: Callable[[Command | list[Command]], Any],
gateway_id: str,
) -> None:
"""Initialize a cover.""" """Initialize a cover."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._attr_unique_id = f"{gateway_id}-{device.id}" self._attr_unique_id = f"{gateway_id}-{device.id}"
@ -29,40 +46,50 @@ class TradfriCover(TradfriBaseDevice, CoverEntity):
self._refresh(device) self._refresh(device)
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, str] | None:
"""Return the state attributes.""" """Return the state attributes."""
return {ATTR_MODEL: self._device.device_info.model_number} return {ATTR_MODEL: self._device.device_info.model_number}
@property @property
def current_cover_position(self): def current_cover_position(self) -> int | None:
"""Return current position of cover. """Return current position of cover.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
return 100 - self._device_data.current_cover_position if not self._device_data:
return None
return 100 - cast(int, self._device_data.current_cover_position)
async def async_set_cover_position(self, **kwargs): async def async_set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position.""" """Move the cover to a specific position."""
if not self._device_control:
return
await self._api(self._device_control.set_state(100 - kwargs[ATTR_POSITION])) await self._api(self._device_control.set_state(100 - kwargs[ATTR_POSITION]))
async def async_open_cover(self, **kwargs): async def async_open_cover(self, **kwargs: Any) -> None:
"""Open the cover.""" """Open the cover."""
if not self._device_control:
return
await self._api(self._device_control.set_state(0)) await self._api(self._device_control.set_state(0))
async def async_close_cover(self, **kwargs): async def async_close_cover(self, **kwargs: Any) -> None:
"""Close cover.""" """Close cover."""
if not self._device_control:
return
await self._api(self._device_control.set_state(100)) await self._api(self._device_control.set_state(100))
async def async_stop_cover(self, **kwargs): async def async_stop_cover(self, **kwargs: Any) -> None:
"""Close cover.""" """Close cover."""
if not self._device_control:
return
await self._api(self._device_control.trigger_blind()) await self._api(self._device_control.trigger_blind())
@property @property
def is_closed(self): def is_closed(self) -> bool:
"""Return if the cover is closed or not.""" """Return if the cover is closed or not."""
return self.current_cover_position == 0 return self.current_cover_position == 0
def _refresh(self, device): def _refresh(self, device: Command) -> None:
"""Refresh the cover data.""" """Refresh the cover data."""
super()._refresh(device) super()._refresh(device)
self._device = device self._device = device