Archived
transforming to integration
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"""Number entities for Boiler Auxiliary Heater Control."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.number import NumberEntity, NumberMode
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from .const import DOMAIN, NUMBER_DEFINITIONS
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up number entities."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
entities = []
|
||||
for key, defn in NUMBER_DEFINITIONS.items():
|
||||
entity = BoilerAuxHeaterNumber(coordinator, entry, key, defn)
|
||||
entities.append(entity)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class BoilerAuxHeaterNumber(RestoreEntity, NumberEntity):
|
||||
"""A configurable number parameter for the boiler aux heater."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_mode = NumberMode.SLIDER
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator,
|
||||
entry: ConfigEntry,
|
||||
key: str,
|
||||
defn: dict,
|
||||
) -> None:
|
||||
"""Initialize the number entity."""
|
||||
self.coordinator = coordinator
|
||||
self._key = key
|
||||
self._defn = defn
|
||||
self._attr_unique_id = f"{entry.entry_id}_{key}"
|
||||
self._attr_translation_key = key
|
||||
self._attr_native_min_value = defn["min"]
|
||||
self._attr_native_max_value = defn["max"]
|
||||
self._attr_native_step = defn["step"]
|
||||
self._attr_native_unit_of_measurement = defn["unit"]
|
||||
self._attr_icon = defn["icon"]
|
||||
self._attr_native_value = defn["default"]
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, entry.entry_id)},
|
||||
name="Boiler Auxiliary Heater",
|
||||
manufacturer="Custom",
|
||||
)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Restore state on startup."""
|
||||
await super().async_added_to_hass()
|
||||
last_state = await self.async_get_last_state()
|
||||
if last_state and last_state.state not in ("unknown", "unavailable"):
|
||||
try:
|
||||
self._attr_native_value = float(last_state.state)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
# Register with coordinator
|
||||
self.coordinator.register_number(self._key, self)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
self._attr_native_value = value
|
||||
self.async_write_ha_state()
|
||||
# Trigger coordinator recalculation
|
||||
self.coordinator.async_request_refresh()
|
||||
Reference in New Issue
Block a user