2022-11-23 16:47:32 +00:00
|
|
|
"""Creates HomeWizard Number entities."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.components.number import NumberEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, EntityCategory
|
2022-11-23 16:47:32 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
2023-01-15 16:30:27 +00:00
|
|
|
from .entity import HomeWizardEntity
|
|
|
|
from .helpers import homewizard_exception_handler
|
2022-11-23 16:47:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up numbers for device."""
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2023-04-13 11:42:35 +00:00
|
|
|
if coordinator.supports_state():
|
2023-01-16 08:23:03 +00:00
|
|
|
async_add_entities([HWEnergyNumberEntity(coordinator, entry)])
|
2022-11-23 16:47:32 +00:00
|
|
|
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
2022-11-23 16:47:32 +00:00
|
|
|
"""Representation of status light number."""
|
|
|
|
|
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
2023-01-16 08:23:03 +00:00
|
|
|
_attr_icon = "mdi:lightbulb-on"
|
2023-06-28 12:02:54 +00:00
|
|
|
_attr_translation_key = "status_light_brightness"
|
2023-01-16 08:23:03 +00:00
|
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
2022-11-23 16:47:32 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: HWEnergyDeviceUpdateCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the control number."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_unique_id = f"{entry.unique_id}_status_light_brightness"
|
|
|
|
|
2023-01-15 16:30:27 +00:00
|
|
|
@homewizard_exception_handler
|
2022-11-23 16:47:32 +00:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
|
|
|
"""Set a new value."""
|
2023-01-16 08:23:03 +00:00
|
|
|
await self.coordinator.api.state_set(brightness=int(value * (255 / 100)))
|
2022-11-23 16:47:32 +00:00
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> float | None:
|
|
|
|
"""Return the current value."""
|
2023-01-16 08:23:03 +00:00
|
|
|
if (
|
|
|
|
self.coordinator.data.state is None
|
|
|
|
or self.coordinator.data.state.brightness is None
|
|
|
|
):
|
2022-11-23 16:47:32 +00:00
|
|
|
return None
|
2023-06-28 12:02:54 +00:00
|
|
|
brightness: float = self.coordinator.data.state.brightness
|
|
|
|
return round(brightness * (100 / 255))
|