Improve type hints in freedompro lights (#76045)
This commit is contained in:
parent
c8f11d65d2
commit
46d3f2e14c
1 changed files with 19 additions and 13 deletions
|
@ -1,5 +1,8 @@
|
||||||
"""Support for Freedompro light."""
|
"""Support for Freedompro light."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from pyfreedompro import put_state
|
from pyfreedompro import put_state
|
||||||
|
|
||||||
|
@ -17,6 +20,7 @@ from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import FreedomproDataUpdateCoordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,8 +28,8 @@ async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Freedompro light."""
|
"""Set up Freedompro light."""
|
||||||
api_key = entry.data[CONF_API_KEY]
|
api_key: str = entry.data[CONF_API_KEY]
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
Device(hass, api_key, device, coordinator)
|
Device(hass, api_key, device, coordinator)
|
||||||
for device in coordinator.data
|
for device in coordinator.data
|
||||||
|
@ -36,7 +40,13 @@ async def async_setup_entry(
|
||||||
class Device(CoordinatorEntity, LightEntity):
|
class Device(CoordinatorEntity, LightEntity):
|
||||||
"""Representation of an Freedompro light."""
|
"""Representation of an Freedompro light."""
|
||||||
|
|
||||||
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 light."""
|
"""Initialize the Freedompro light."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._session = aiohttp_client.async_get_clientsession(hass)
|
self._session = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
@ -44,9 +54,7 @@ class Device(CoordinatorEntity, LightEntity):
|
||||||
self._attr_name = device["name"]
|
self._attr_name = device["name"]
|
||||||
self._attr_unique_id = device["uid"]
|
self._attr_unique_id = device["uid"]
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={
|
identifiers={(DOMAIN, device["uid"])},
|
||||||
(DOMAIN, self.unique_id),
|
|
||||||
},
|
|
||||||
manufacturer="Freedompro",
|
manufacturer="Freedompro",
|
||||||
model=device["type"],
|
model=device["type"],
|
||||||
name=self.name,
|
name=self.name,
|
||||||
|
@ -87,31 +95,29 @@ class Device(CoordinatorEntity, LightEntity):
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self._handle_coordinator_update()
|
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 light."""
|
"""Async function to set on to light."""
|
||||||
payload = {"on": True}
|
payload: dict[str, Any] = {"on": True}
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
payload["brightness"] = round(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
|
payload["brightness"] = round(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
|
||||||
if ATTR_HS_COLOR in kwargs:
|
if ATTR_HS_COLOR in kwargs:
|
||||||
payload["saturation"] = round(kwargs[ATTR_HS_COLOR][1])
|
payload["saturation"] = round(kwargs[ATTR_HS_COLOR][1])
|
||||||
payload["hue"] = round(kwargs[ATTR_HS_COLOR][0])
|
payload["hue"] = round(kwargs[ATTR_HS_COLOR][0])
|
||||||
payload = json.dumps(payload)
|
|
||||||
await put_state(
|
await put_state(
|
||||||
self._session,
|
self._session,
|
||||||
self._api_key,
|
self._api_key,
|
||||||
self._attr_unique_id,
|
self._attr_unique_id,
|
||||||
payload,
|
json.dumps(payload),
|
||||||
)
|
)
|
||||||
await self.coordinator.async_request_refresh()
|
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 light."""
|
"""Async function to set off to light."""
|
||||||
payload = {"on": False}
|
payload = {"on": False}
|
||||||
payload = json.dumps(payload)
|
|
||||||
await put_state(
|
await put_state(
|
||||||
self._session,
|
self._session,
|
||||||
self._api_key,
|
self._api_key,
|
||||||
self._attr_unique_id,
|
self._attr_unique_id,
|
||||||
payload,
|
json.dumps(payload),
|
||||||
)
|
)
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue