|
| 1 | +"""Binary sensors for Midea Heat Pump diagnostic state registers.""" |
| 2 | +import logging |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from homeassistant.components.binary_sensor import ( |
| 6 | + BinarySensorDeviceClass, |
| 7 | + BinarySensorEntity, |
| 8 | +) |
| 9 | +from homeassistant.config_entries import ConfigEntry |
| 10 | +from homeassistant.core import HomeAssistant, callback |
| 11 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 12 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 13 | + |
| 14 | +from .const import ( |
| 15 | + DOMAIN, |
| 16 | + CONF_MODBUS_UNIT, |
| 17 | + CONF_HEATER_ASSIST_REGISTER, |
| 18 | + CONF_SANITIZE_STATE_REGISTER, |
| 19 | +) |
| 20 | +from .coordinator import MideaModbusCoordinator |
| 21 | + |
| 22 | +_LOGGER = logging.getLogger(__name__) |
| 23 | + |
| 24 | +# Register 109 values that indicate an active sanitize cycle |
| 25 | +_SANITIZE_ACTIVE_VALUES = {32, 33} |
| 26 | + |
| 27 | + |
| 28 | +async def async_setup_entry( |
| 29 | + hass: HomeAssistant, |
| 30 | + config_entry: ConfigEntry, |
| 31 | + async_add_entities: AddEntitiesCallback, |
| 32 | +) -> None: |
| 33 | + """Set up Midea diagnostic binary sensors from config entry.""" |
| 34 | + coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"] |
| 35 | + config = hass.data[DOMAIN][config_entry.entry_id]["config"] |
| 36 | + |
| 37 | + host_suffix = f" ({config['host']})" |
| 38 | + entities = [] |
| 39 | + |
| 40 | + if config.get(CONF_HEATER_ASSIST_REGISTER) is not None: |
| 41 | + entities.append( |
| 42 | + MideaBinarySensor( |
| 43 | + coordinator=coordinator, |
| 44 | + config=config, |
| 45 | + data_key="heater_assist_raw", |
| 46 | + name=f"Heater Assist{host_suffix}", |
| 47 | + register=config[CONF_HEATER_ASSIST_REGISTER], |
| 48 | + device_class=BinarySensorDeviceClass.RUNNING, |
| 49 | + is_on_fn=lambda v: v != 0, |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + if config.get(CONF_SANITIZE_STATE_REGISTER) is not None: |
| 54 | + entities.append( |
| 55 | + MideaBinarySensor( |
| 56 | + coordinator=coordinator, |
| 57 | + config=config, |
| 58 | + data_key="sanitize_state_raw", |
| 59 | + name=f"Sanitize Cycle Active{host_suffix}", |
| 60 | + register=config[CONF_SANITIZE_STATE_REGISTER], |
| 61 | + device_class=BinarySensorDeviceClass.RUNNING, |
| 62 | + is_on_fn=lambda v: v in _SANITIZE_ACTIVE_VALUES, |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + async_add_entities(entities) |
| 67 | + |
| 68 | + |
| 69 | +class MideaBinarySensor(CoordinatorEntity, BinarySensorEntity): |
| 70 | + """Read-only binary sensor derived from a raw diagnostic register value.""" |
| 71 | + |
| 72 | + def __init__( |
| 73 | + self, |
| 74 | + coordinator: MideaModbusCoordinator, |
| 75 | + config: dict, |
| 76 | + data_key: str, |
| 77 | + name: str, |
| 78 | + register: int, |
| 79 | + device_class: BinarySensorDeviceClass, |
| 80 | + is_on_fn, |
| 81 | + ) -> None: |
| 82 | + """Initialize the binary sensor.""" |
| 83 | + super().__init__(coordinator) |
| 84 | + self._config = config |
| 85 | + self._data_key = data_key |
| 86 | + self._register = register |
| 87 | + self._is_on_fn = is_on_fn |
| 88 | + |
| 89 | + self._attr_name = name |
| 90 | + self._attr_unique_id = f"midea_{config['host']}_{config[CONF_MODBUS_UNIT]}_{data_key}" |
| 91 | + self._attr_device_class = device_class |
| 92 | + |
| 93 | + @property |
| 94 | + def device_info(self) -> dict[str, Any]: |
| 95 | + """Return device info to group this sensor with the main device.""" |
| 96 | + return { |
| 97 | + "identifiers": {(DOMAIN, f"{self._config['host']}_{self._config[CONF_MODBUS_UNIT]}")}, |
| 98 | + "name": f"Midea Heat Pump ({self._config['host']})", |
| 99 | + "manufacturer": "Midea", |
| 100 | + "model": "Heat Pump Water Heater", |
| 101 | + } |
| 102 | + |
| 103 | + @property |
| 104 | + def is_on(self) -> bool | None: |
| 105 | + """Return True when the register value indicates active state.""" |
| 106 | + raw = (self.coordinator.data or {}).get(self._data_key) |
| 107 | + if raw is None: |
| 108 | + return None |
| 109 | + return self._is_on_fn(raw) |
| 110 | + |
| 111 | + @property |
| 112 | + def available(self) -> bool: |
| 113 | + """Return if entity is available.""" |
| 114 | + return self.coordinator.last_update_success and self._data_key in (self.coordinator.data or {}) |
| 115 | + |
| 116 | + @callback |
| 117 | + def _handle_coordinator_update(self) -> None: |
| 118 | + """Handle updated data from the coordinator, logging raw register value.""" |
| 119 | + raw = (self.coordinator.data or {}).get(self._data_key) |
| 120 | + _LOGGER.debug( |
| 121 | + "%s: register %s raw=%s -> is_on=%s", |
| 122 | + self._attr_name, |
| 123 | + self._register, |
| 124 | + raw, |
| 125 | + self._is_on_fn(raw) if raw is not None else None, |
| 126 | + ) |
| 127 | + self.async_write_ha_state() |
0 commit comments