Improve type hint in freedompro entities (#77170)

This commit is contained in:
epenet 2022-08-26 10:48:12 +02:00 committed by GitHub
parent 8896229ea6
commit 8f9ff0f88e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 93 additions and 57 deletions

View file

@ -1,4 +1,6 @@
"""Support for Freedompro binary_sensor."""
from typing import Any
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
@ -9,6 +11,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
DEVICE_CLASS_MAP = {
@ -32,7 +35,7 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro binary_sensor."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(device, coordinator)
for device in coordinator.data
@ -43,7 +46,9 @@ async def async_setup_entry(
class Device(CoordinatorEntity, BinarySensorEntity):
"""Representation of an Freedompro binary_sensor."""
def __init__(self, device, coordinator):
def __init__(
self, device: dict[str, Any], coordinator: FreedomproDataUpdateCoordinator
) -> None:
"""Initialize the Freedompro binary_sensor."""
super().__init__(coordinator)
self._attr_name = device["name"]
@ -51,7 +56,7 @@ class Device(CoordinatorEntity, BinarySensorEntity):
self._type = device["type"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],

View file

@ -3,7 +3,9 @@ from __future__ import annotations
import json
import logging
from typing import Any
from aiohttp.client import ClientSession
from pyfreedompro import put_state
from homeassistant.components.climate import ClimateEntity
@ -20,6 +22,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -43,8 +46,8 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro climate."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id]
api_key: str = entry.data[CONF_API_KEY]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(
aiohttp_client.async_get_clientsession(hass), api_key, device, coordinator
@ -54,13 +57,19 @@ async def async_setup_entry(
)
class Device(CoordinatorEntity, ClimateEntity):
class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], ClimateEntity):
"""Representation of an Freedompro climate."""
_attr_hvac_modes = SUPPORTED_HVAC_MODES
_attr_temperature_unit = TEMP_CELSIUS
def __init__(self, session, api_key, device, coordinator):
def __init__(
self,
session: ClientSession,
api_key: str,
device: dict[str, Any],
coordinator: FreedomproDataUpdateCoordinator,
) -> None:
"""Initialize the Freedompro climate."""
super().__init__(coordinator)
self._session = session
@ -70,7 +79,7 @@ class Device(CoordinatorEntity, ClimateEntity):
self._characteristics = device["characteristics"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],
@ -107,23 +116,22 @@ class Device(CoordinatorEntity, ClimateEntity):
await super().async_added_to_hass()
self._handle_coordinator_update()
async def async_set_hvac_mode(self, hvac_mode):
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Async function to set mode to climate."""
if hvac_mode not in SUPPORTED_HVAC_MODES:
raise ValueError(f"Got unsupported hvac_mode {hvac_mode}")
payload = {}
payload["heatingCoolingState"] = HVAC_INVERT_MAP[hvac_mode]
payload = json.dumps(payload)
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()
async def async_set_temperature(self, **kwargs):
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Async function to set temperature to climate."""
payload = {}
if ATTR_HVAC_MODE in kwargs:
@ -137,11 +145,10 @@ class Device(CoordinatorEntity, ClimateEntity):
payload["heatingCoolingState"] = HVAC_INVERT_MAP[kwargs[ATTR_HVAC_MODE]]
if ATTR_TEMPERATURE in kwargs:
payload["targetTemperature"] = kwargs[ATTR_TEMPERATURE]
payload = json.dumps(payload)
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()

View file

@ -18,6 +18,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
DEVICE_CLASS_MAP = {
@ -35,8 +36,8 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro cover."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id]
api_key: str = entry.data[CONF_API_KEY]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(hass, api_key, device, coordinator)
for device in coordinator.data
@ -47,7 +48,13 @@ async def async_setup_entry(
class Device(CoordinatorEntity, CoverEntity):
"""Representation of an Freedompro cover."""
def __init__(self, hass, api_key, device, coordinator):
def __init__(
self,
hass: HomeAssistant,
api_key: str,
device: dict[str, Any],
coordinator: FreedomproDataUpdateCoordinator,
) -> None:
"""Initialize the Freedompro cover."""
super().__init__(coordinator)
self._session = aiohttp_client.async_get_clientsession(hass)
@ -56,7 +63,7 @@ class Device(CoordinatorEntity, CoverEntity):
self._attr_unique_id = device["uid"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],

View file

@ -15,6 +15,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
@ -22,8 +23,8 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro fan."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id]
api_key: str = entry.data[CONF_API_KEY]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
FreedomproFan(hass, api_key, device, coordinator)
for device in coordinator.data
@ -31,10 +32,16 @@ async def async_setup_entry(
)
class FreedomproFan(CoordinatorEntity, FanEntity):
class FreedomproFan(CoordinatorEntity[FreedomproDataUpdateCoordinator], FanEntity):
"""Representation of an Freedompro fan."""
def __init__(self, hass, api_key, device, coordinator):
def __init__(
self,
hass: HomeAssistant,
api_key: str,
device: dict[str, Any],
coordinator: FreedomproDataUpdateCoordinator,
) -> None:
"""Initialize the Freedompro fan."""
super().__init__(coordinator)
self._session = aiohttp_client.async_get_clientsession(hass)
@ -44,7 +51,7 @@ class FreedomproFan(CoordinatorEntity, FanEntity):
self._characteristics = device["characteristics"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],
@ -60,11 +67,6 @@ class FreedomproFan(CoordinatorEntity, FanEntity):
"""Return True if entity is on."""
return self._attr_is_on
@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""
return self._attr_percentage
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
@ -117,12 +119,11 @@ class FreedomproFan(CoordinatorEntity, FanEntity):
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
rotation_speed = {"rotationSpeed": percentage}
payload = json.dumps(rotation_speed)
payload = {"rotationSpeed": percentage}
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()

View file

@ -13,6 +13,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
@ -20,8 +21,8 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro lock."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id]
api_key: str = entry.data[CONF_API_KEY]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(hass, api_key, device, coordinator)
for device in coordinator.data
@ -29,10 +30,16 @@ async def async_setup_entry(
)
class Device(CoordinatorEntity, LockEntity):
class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], LockEntity):
"""Representation of an Freedompro lock."""
def __init__(self, hass, api_key, device, coordinator):
def __init__(
self,
hass: HomeAssistant,
api_key: str,
device: dict[str, Any],
coordinator: FreedomproDataUpdateCoordinator,
) -> None:
"""Initialize the Freedompro lock."""
super().__init__(coordinator)
self._hass = hass
@ -44,7 +51,7 @@ class Device(CoordinatorEntity, LockEntity):
self._characteristics = device["characteristics"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=self._type,
@ -78,24 +85,22 @@ class Device(CoordinatorEntity, LockEntity):
async def async_lock(self, **kwargs: Any) -> None:
"""Async function to lock the lock."""
payload_dict = {"lock": 1}
payload = json.dumps(payload_dict)
payload = {"lock": 1}
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()
async def async_unlock(self, **kwargs: Any) -> None:
"""Async function to unlock the lock."""
payload_dict = {"lock": 0}
payload = json.dumps(payload_dict)
payload = {"lock": 0}
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()

View file

@ -1,4 +1,6 @@
"""Support for Freedompro sensor."""
from typing import Any
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@ -11,6 +13,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
DEVICE_CLASS_MAP = {
@ -40,7 +43,7 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro sensor."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(device, coordinator)
for device in coordinator.data
@ -48,10 +51,12 @@ async def async_setup_entry(
)
class Device(CoordinatorEntity, SensorEntity):
class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], SensorEntity):
"""Representation of an Freedompro sensor."""
def __init__(self, device, coordinator):
def __init__(
self, device: dict[str, Any], coordinator: FreedomproDataUpdateCoordinator
) -> None:
"""Initialize the Freedompro sensor."""
super().__init__(coordinator)
self._attr_name = device["name"]
@ -59,7 +64,7 @@ class Device(CoordinatorEntity, SensorEntity):
self._type = device["type"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],

View file

@ -1,5 +1,6 @@
"""Support for Freedompro switch."""
import json
from typing import Any
from pyfreedompro import put_state
@ -12,6 +13,7 @@ from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FreedomproDataUpdateCoordinator
from .const import DOMAIN
@ -19,8 +21,8 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Freedompro switch."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id]
api_key: str = entry.data[CONF_API_KEY]
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
Device(hass, api_key, device, coordinator)
for device in coordinator.data
@ -28,10 +30,16 @@ async def async_setup_entry(
)
class Device(CoordinatorEntity, SwitchEntity):
class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], SwitchEntity):
"""Representation of an Freedompro switch."""
def __init__(self, hass, api_key, device, coordinator):
def __init__(
self,
hass: HomeAssistant,
api_key: str,
device: dict[str, Any],
coordinator: FreedomproDataUpdateCoordinator,
) -> None:
"""Initialize the Freedompro switch."""
super().__init__(coordinator)
self._session = aiohttp_client.async_get_clientsession(hass)
@ -40,7 +48,7 @@ class Device(CoordinatorEntity, SwitchEntity):
self._attr_unique_id = device["uid"]
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, self.unique_id),
(DOMAIN, device["uid"]),
},
manufacturer="Freedompro",
model=device["type"],
@ -70,26 +78,24 @@ class Device(CoordinatorEntity, SwitchEntity):
await super().async_added_to_hass()
self._handle_coordinator_update()
async def async_turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Async function to set on to switch."""
payload = {"on": True}
payload = json.dumps(payload)
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Async function to set off to switch."""
payload = {"on": False}
payload = json.dumps(payload)
await put_state(
self._session,
self._api_key,
self.unique_id,
payload,
json.dumps(payload),
)
await self.coordinator.async_request_refresh()