ha-hunterbtt

Home Assistant integration for Hunter BTT
git clone https://git.stephank.nl/ha-hunterbtt
Log | Files | Refs | README | LICENSE | ZIP

number.py (2418B)


      1 """Manual duration control for Hunter BTT."""
      2 
      3 from homeassistant.components.number import NumberDeviceClass, NumberMode, RestoreNumber
      4 from homeassistant.const import UnitOfTime
      5 from homeassistant.core import HomeAssistant
      6 from homeassistant.helpers.entity_platform import AddEntitiesCallback
      7 
      8 from .const import (
      9     DEFAULT_MANUAL_DURATION_MINUTES,
     10     MAX_MANUAL_DURATION_MINUTES,
     11     MIN_MANUAL_DURATION_MINUTES,
     12 )
     13 from .coordinator import HunterBTTConfigEntry, HunterBTTCoordinator
     14 from .entity import HunterBTTEntity
     15 
     16 
     17 async def async_setup_entry(
     18     _hass: HomeAssistant,
     19     entry: HunterBTTConfigEntry,
     20     async_add_entities: AddEntitiesCallback,
     21 ) -> None:
     22     """Set up manual-duration controls for supported BTT channels."""
     23     coordinator = entry.runtime_data
     24     if coordinator.data.state.channel_count != 1:
     25         return
     26     async_add_entities([HunterBTTManualDurationNumber(coordinator)])
     27 
     28 
     29 class HunterBTTManualDurationNumber(HunterBTTEntity, RestoreNumber):
     30     """Store the duration to use for the next HA-initiated zone-1 run."""
     31 
     32     _attr_translation_key = "manual_duration"
     33     _attr_device_class = NumberDeviceClass.DURATION
     34     _attr_mode = NumberMode.BOX
     35     _attr_native_min_value = MIN_MANUAL_DURATION_MINUTES
     36     _attr_native_max_value = MAX_MANUAL_DURATION_MINUTES
     37     _attr_native_step = 1
     38     _attr_native_unit_of_measurement = UnitOfTime.MINUTES
     39 
     40     def __init__(self, coordinator: HunterBTTCoordinator) -> None:
     41         """Initialize with the safe default until a value is restored."""
     42         super().__init__(coordinator, "zone_1_manual_duration")
     43         self._attr_native_value: float = DEFAULT_MANUAL_DURATION_MINUTES
     44 
     45     async def async_added_to_hass(self) -> None:
     46         """Restore the selected duration without writing valve configuration."""
     47         await super().async_added_to_hass()
     48         if (
     49             last_number_data := await self.async_get_last_number_data()
     50         ) is not None and last_number_data.native_value is not None:
     51             self._attr_native_value = last_number_data.native_value
     52         self.coordinator.manual_duration_minutes = int(self._attr_native_value)
     53 
     54     async def async_set_native_value(self, value: float) -> None:
     55         """Store a whole-minute duration for the next manual start."""
     56         self._attr_native_value = value
     57         self.coordinator.manual_duration_minutes = int(value)
     58         self.async_write_ha_state()