"""Binary sensor entities for the Xiaoxiang Smart BMS integration.""" from __future__ import annotations from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN from .coordinator import BmsCoordinator # --------------------------------------------------------------------------- # Entity descriptions # --------------------------------------------------------------------------- BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = ( # -- MOS gate status -------------------------------------------------- BinarySensorEntityDescription( key="mos_charge_enabled", name="Charge MOSFET", device_class=BinarySensorDeviceClass.BATTERY_CHARGING, icon="mdi:battery-charging", ), BinarySensorEntityDescription( key="mos_discharge_enabled", name="Discharge MOSFET", device_class=BinarySensorDeviceClass.POWER, icon="mdi:power-plug", ), # -- Cell balancing --------------------------------------------------- BinarySensorEntityDescription( key="balance_active", name="Cell Balancing", device_class=BinarySensorDeviceClass.RUNNING, icon="mdi:battery-sync", ), # -- Protection flags (True = protection triggered = problem) --------- BinarySensorEntityDescription( key="prot_cell_overvolt", name="Cell Over-Voltage", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:battery-arrow-up", ), BinarySensorEntityDescription( key="prot_cell_undervolt", name="Cell Under-Voltage", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:battery-arrow-down", ), BinarySensorEntityDescription( key="prot_pack_overvolt", name="Pack Over-Voltage", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:battery-arrow-up", ), BinarySensorEntityDescription( key="prot_pack_undervolt", name="Pack Under-Voltage", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:battery-arrow-down", ), BinarySensorEntityDescription( key="prot_charge_overtemp", name="Charge Over-Temperature", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:thermometer-alert", ), BinarySensorEntityDescription( key="prot_charge_undertemp", name="Charge Under-Temperature", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:thermometer-alert", ), BinarySensorEntityDescription( key="prot_discharge_overtemp", name="Discharge Over-Temperature", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:thermometer-alert", ), BinarySensorEntityDescription( key="prot_discharge_undertemp", name="Discharge Under-Temperature", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:thermometer-alert", ), BinarySensorEntityDescription( key="prot_charge_overcurrent", name="Charge Over-Current", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:current-ac", ), BinarySensorEntityDescription( key="prot_discharge_overcurrent", name="Discharge Over-Current", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:current-ac", ), BinarySensorEntityDescription( key="prot_short_circuit", name="Short Circuit", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:flash-alert", ), BinarySensorEntityDescription( key="prot_frontend_ic_error", name="Frontend IC Error", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:chip", ), BinarySensorEntityDescription( key="prot_software_lock", name="Software Lock", device_class=BinarySensorDeviceClass.PROBLEM, icon="mdi:lock-alert", ), ) # --------------------------------------------------------------------------- # Platform setup # --------------------------------------------------------------------------- async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: coordinator: BmsCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( BmsBinarySensor(coordinator, description) for description in BINARY_SENSORS ) # --------------------------------------------------------------------------- # Entity class # --------------------------------------------------------------------------- class BmsBinarySensor(CoordinatorEntity[BmsCoordinator], BinarySensorEntity): """A binary sensor backed by a boolean key in coordinator.data.""" _attr_has_entity_name = True def __init__( self, coordinator: BmsCoordinator, description: BinarySensorEntityDescription, ) -> None: super().__init__(coordinator) self.entity_description = description self._attr_unique_id = f"{coordinator.address}_{description.key}" self._attr_device_info = coordinator.device_info @property def is_on(self) -> bool | None: return self.coordinator.data.get(self.entity_description.key)