|
| 1 | +"""Support for the GCE Eco-Devices binary sensors.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from pyecodevices import EcoDevices |
| 6 | + |
| 7 | +from homeassistant.components.binary_sensor import BinarySensorEntity |
| 8 | +from homeassistant.config_entries import ConfigEntry |
| 9 | +from homeassistant.core import HomeAssistant |
| 10 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 11 | +from homeassistant.helpers.update_coordinator import ( |
| 12 | + CoordinatorEntity, |
| 13 | + DataUpdateCoordinator, |
| 14 | +) |
| 15 | +from homeassistant.util import slugify |
| 16 | + |
| 17 | +from .const import ( |
| 18 | + CONF_T1_ENABLED, |
| 19 | + CONF_T1_TYPE, |
| 20 | + CONF_T2_ENABLED, |
| 21 | + CONF_T2_TYPE, |
| 22 | + CONF_TI_TYPE_HCHP, |
| 23 | + CONF_TI_TYPE_TEMPO, |
| 24 | + CONTROLLER, |
| 25 | + COORDINATOR, |
| 26 | + DEFAULT_T1_NAME, |
| 27 | + DEFAULT_T2_NAME, |
| 28 | + DOMAIN, |
| 29 | +) |
| 30 | +from .entity import get_device_info |
| 31 | + |
| 32 | +_LOGGER = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +async def async_setup_entry( |
| 36 | + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback |
| 37 | +) -> None: |
| 38 | + """Set up the GCE Eco-Devices platform.""" |
| 39 | + data = hass.data[DOMAIN][entry.entry_id] |
| 40 | + controller = data[CONTROLLER] |
| 41 | + coordinator = data[COORDINATOR] |
| 42 | + config = entry.data |
| 43 | + options = entry.options |
| 44 | + |
| 45 | + t1_enabled = options.get(CONF_T1_ENABLED, config.get(CONF_T1_ENABLED)) |
| 46 | + t1_type = options.get(CONF_T1_TYPE, config.get(CONF_T1_TYPE)) |
| 47 | + t2_enabled = options.get(CONF_T2_ENABLED, config.get(CONF_T2_ENABLED)) |
| 48 | + t2_type = options.get(CONF_T2_TYPE, config.get(CONF_T2_TYPE)) |
| 49 | + |
| 50 | + entities: list[BinarySensorEntity] = [] |
| 51 | + |
| 52 | + for input_number in (1, 2): |
| 53 | + if t1_enabled and input_number == 1 or t2_enabled and input_number == 2: |
| 54 | + _LOGGER.debug("Add the teleinfo %s binary_sensor entities", input_number) |
| 55 | + if t1_type in (CONF_TI_TYPE_HCHP, CONF_TI_TYPE_TEMPO) or t2_type in ( |
| 56 | + CONF_TI_TYPE_HCHP, |
| 57 | + CONF_TI_TYPE_TEMPO, |
| 58 | + ): |
| 59 | + prefix_name = DEFAULT_T1_NAME if input_number == 1 else DEFAULT_T2_NAME |
| 60 | + entities.append( |
| 61 | + TeleinfoInputHeuresCreuses( |
| 62 | + controller, |
| 63 | + coordinator, |
| 64 | + input_number=input_number, |
| 65 | + input_name="PTEC", |
| 66 | + name=f"{prefix_name} Heures Creuses", |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + if entities: |
| 71 | + async_add_entities(entities) |
| 72 | + |
| 73 | + |
| 74 | +class TeleinfoInputHeuresCreuses(CoordinatorEntity, BinarySensorEntity): |
| 75 | + """Representation of a Eco-Devices HC binary sensor.""" |
| 76 | + |
| 77 | + _attr_icon = "mdi:cash-clock" |
| 78 | + |
| 79 | + def __init__( |
| 80 | + self, |
| 81 | + controller: EcoDevices, |
| 82 | + coordinator: DataUpdateCoordinator, |
| 83 | + input_number: int, |
| 84 | + input_name: str, |
| 85 | + name: str, |
| 86 | + ) -> None: |
| 87 | + """Initialize the binary sensor.""" |
| 88 | + super().__init__(coordinator) |
| 89 | + self.controller = controller |
| 90 | + self._input_number = input_number |
| 91 | + self._input_name = input_name |
| 92 | + self._attr_name = name |
| 93 | + self._attr_unique_id = slugify( |
| 94 | + f"{DOMAIN}_{self.controller.mac_address}_binary_sensor_{self._input_number}_{self._input_name}" |
| 95 | + ) |
| 96 | + self._attr_device_info = get_device_info(self.controller) |
| 97 | + |
| 98 | + @property |
| 99 | + def is_on(self) -> bool | None: |
| 100 | + """Return the state.""" |
| 101 | + if type_heure := self.coordinator.data.get( |
| 102 | + f"T{self._input_number}_{self._input_name}" |
| 103 | + ): |
| 104 | + if type_heure.startswith("HC"): |
| 105 | + return True |
| 106 | + return False |
| 107 | + return None |
0 commit comments