|
| 1 | +"""Number entities for the Seko PoolDose integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import TYPE_CHECKING, Any, cast |
| 7 | + |
| 8 | +from homeassistant.components.number import ( |
| 9 | + NumberDeviceClass, |
| 10 | + NumberEntity, |
| 11 | + NumberEntityDescription, |
| 12 | +) |
| 13 | +from homeassistant.const import ( |
| 14 | + CONCENTRATION_PARTS_PER_MILLION, |
| 15 | + EntityCategory, |
| 16 | + UnitOfElectricPotential, |
| 17 | +) |
| 18 | +from homeassistant.core import HomeAssistant |
| 19 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 20 | + |
| 21 | +from . import PooldoseConfigEntry |
| 22 | +from .entity import PooldoseEntity |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from .coordinator import PooldoseCoordinator |
| 26 | + |
| 27 | +_LOGGER = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +NUMBER_DESCRIPTIONS: tuple[NumberEntityDescription, ...] = ( |
| 31 | + NumberEntityDescription( |
| 32 | + key="ph_target", |
| 33 | + translation_key="ph_target", |
| 34 | + entity_category=EntityCategory.CONFIG, |
| 35 | + device_class=NumberDeviceClass.PH, |
| 36 | + ), |
| 37 | + NumberEntityDescription( |
| 38 | + key="orp_target", |
| 39 | + translation_key="orp_target", |
| 40 | + entity_category=EntityCategory.CONFIG, |
| 41 | + device_class=NumberDeviceClass.VOLTAGE, |
| 42 | + native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT, |
| 43 | + ), |
| 44 | + NumberEntityDescription( |
| 45 | + key="cl_target", |
| 46 | + translation_key="cl_target", |
| 47 | + entity_category=EntityCategory.CONFIG, |
| 48 | + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, |
| 49 | + ), |
| 50 | + NumberEntityDescription( |
| 51 | + key="ofa_ph_lower", |
| 52 | + translation_key="ofa_ph_lower", |
| 53 | + entity_category=EntityCategory.CONFIG, |
| 54 | + device_class=NumberDeviceClass.PH, |
| 55 | + ), |
| 56 | + NumberEntityDescription( |
| 57 | + key="ofa_ph_upper", |
| 58 | + translation_key="ofa_ph_upper", |
| 59 | + entity_category=EntityCategory.CONFIG, |
| 60 | + device_class=NumberDeviceClass.PH, |
| 61 | + ), |
| 62 | + NumberEntityDescription( |
| 63 | + key="ofa_orp_lower", |
| 64 | + translation_key="ofa_orp_lower", |
| 65 | + entity_category=EntityCategory.CONFIG, |
| 66 | + device_class=NumberDeviceClass.VOLTAGE, |
| 67 | + native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT, |
| 68 | + ), |
| 69 | + NumberEntityDescription( |
| 70 | + key="ofa_orp_upper", |
| 71 | + translation_key="ofa_orp_upper", |
| 72 | + entity_category=EntityCategory.CONFIG, |
| 73 | + device_class=NumberDeviceClass.VOLTAGE, |
| 74 | + native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT, |
| 75 | + ), |
| 76 | + NumberEntityDescription( |
| 77 | + key="ofa_cl_lower", |
| 78 | + translation_key="ofa_cl_lower", |
| 79 | + entity_category=EntityCategory.CONFIG, |
| 80 | + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, |
| 81 | + ), |
| 82 | + NumberEntityDescription( |
| 83 | + key="ofa_cl_upper", |
| 84 | + translation_key="ofa_cl_upper", |
| 85 | + entity_category=EntityCategory.CONFIG, |
| 86 | + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, |
| 87 | + ), |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +async def async_setup_entry( |
| 92 | + hass: HomeAssistant, |
| 93 | + config_entry: PooldoseConfigEntry, |
| 94 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 95 | +) -> None: |
| 96 | + """Set up PoolDose number entities from a config entry.""" |
| 97 | + if TYPE_CHECKING: |
| 98 | + assert config_entry.unique_id is not None |
| 99 | + |
| 100 | + coordinator = config_entry.runtime_data |
| 101 | + number_data = coordinator.data.get("number", {}) |
| 102 | + serial_number = config_entry.unique_id |
| 103 | + |
| 104 | + async_add_entities( |
| 105 | + PooldoseNumber(coordinator, serial_number, coordinator.device_info, description) |
| 106 | + for description in NUMBER_DESCRIPTIONS |
| 107 | + if description.key in number_data |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +class PooldoseNumber(PooldoseEntity, NumberEntity): |
| 112 | + """Number entity for the Seko PoolDose Python API.""" |
| 113 | + |
| 114 | + def __init__( |
| 115 | + self, |
| 116 | + coordinator: PooldoseCoordinator, |
| 117 | + serial_number: str, |
| 118 | + device_info: Any, |
| 119 | + description: NumberEntityDescription, |
| 120 | + ) -> None: |
| 121 | + """Initialize the number.""" |
| 122 | + super().__init__(coordinator, serial_number, device_info, description, "number") |
| 123 | + self._async_update_attrs() |
| 124 | + |
| 125 | + def _handle_coordinator_update(self) -> None: |
| 126 | + """Handle updated data from the coordinator.""" |
| 127 | + self._async_update_attrs() |
| 128 | + super()._handle_coordinator_update() |
| 129 | + |
| 130 | + def _async_update_attrs(self) -> None: |
| 131 | + """Update number attributes.""" |
| 132 | + data = cast(dict, self.get_data()) |
| 133 | + self._attr_native_value = data["value"] |
| 134 | + self._attr_native_min_value = data["min"] |
| 135 | + self._attr_native_max_value = data["max"] |
| 136 | + self._attr_native_step = data["step"] |
| 137 | + |
| 138 | + async def async_set_native_value(self, value: float) -> None: |
| 139 | + """Set new value.""" |
| 140 | + await self.coordinator.client.set_number(self.entity_description.key, value) |
| 141 | + self._attr_native_value = value |
| 142 | + self.async_write_ha_state() |
0 commit comments