diff --git a/homeassistant/components/freebox/sensor.py b/homeassistant/components/freebox/sensor.py index 7290ce47c04..d065907a914 100644 --- a/homeassistant/components/freebox/sensor.py +++ b/homeassistant/components/freebox/sensor.py @@ -18,6 +18,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util from .const import DOMAIN +from .home_base import FreeboxHomeEntity from .router import FreeboxRouter _LOGGER = logging.getLogger(__name__) @@ -62,7 +63,7 @@ async def async_setup_entry( ) -> None: """Set up the sensors.""" router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] - entities = [] + entities: list[SensorEntity] = [] _LOGGER.debug( "%s - %s - %s temperature sensors", @@ -98,7 +99,17 @@ async def async_setup_entry( for description in DISK_PARTITION_SENSORS ) - async_add_entities(entities, True) + for node in router.home_devices.values(): + for endpoint in node["show_endpoints"]: + if ( + endpoint["name"] == "battery" + and endpoint["ep_type"] == "signal" + and endpoint.get("value") is not None + ): + entities.append(FreeboxBatterySensor(hass, router, node, endpoint)) + + if entities: + async_add_entities(entities, True) class FreeboxSensor(SensorEntity): @@ -125,7 +136,7 @@ class FreeboxSensor(SensorEntity): self._attr_native_value = state @callback - def async_on_demand_update(self): + def async_on_demand_update(self) -> None: """Update state.""" self.async_update_state() self.async_write_ha_state() @@ -213,3 +224,15 @@ class FreeboxDiskSensor(FreeboxSensor): self._partition["free_bytes"] * 100 / self._partition["total_bytes"], 2 ) self._attr_native_value = value + + +class FreeboxBatterySensor(FreeboxHomeEntity, SensorEntity): + """Representation of a Freebox battery sensor.""" + + _attr_device_class = SensorDeviceClass.BATTERY + _attr_native_unit_of_measurement = PERCENTAGE + + @property + def native_value(self) -> int: + """Return the current state of the device.""" + return self.get_value("signal", "battery") diff --git a/tests/components/freebox/const.py b/tests/components/freebox/const.py index 7028366d02b..a6253dbf315 100644 --- a/tests/components/freebox/const.py +++ b/tests/components/freebox/const.py @@ -1995,8 +1995,58 @@ DATA_HOME_GET_NODES = [ "Gateway": 1, "ItemId": "e76c2b75a4a6e2", }, - "show_endpoints": [{...}, {...}, {...}, {...}], - "signal_links": [{...}], + "show_endpoints": [ + { + "category": "", + "ep_type": "slot", + "id": 0, + "label": "Activé", + "name": "enable", + "value": False, + "value_type": "bool", + "visibility": "normal", + }, + { + "category": "", + "ep_type": "signal", + "id": 1, + "label": "Activé", + "name": "enable", + "value": True, + "value_type": "bool", + "visibility": "normal", + }, + { + "category": "", + "ep_type": "signal", + "id": 2, + "label": "Bouton appuyé", + "name": "pushed", + "value": None, + "value_type": "int", + }, + { + "category": "", + "ep_type": "signal", + "id": 3, + "label": "Niveau de Batterie", + "name": "battery", + "refresh": 2000, + "value": 100, + "value_type": "int", + }, + ], + "signal_links": [ + { + "adapter": 5, + "category": "alarm", + "id": 7, + "label": "Système d alarme", + "link_id": 10, + "name": "node_7", + "status": "active", + }, + ], "slot_links": [], "status": "active", "type": { @@ -2081,12 +2131,119 @@ DATA_HOME_GET_NODES = [ "visibility": "normal", }, ], - "signal_links": [{...}], + "signal_links": [ + { + "adapter": 5, + "category": "alarm", + "id": 7, + "label": "Système d alarme", + "link_id": 12, + "name": "node_7", + "status": "active", + } + ], "slot_links": [], "status": "active", "type": { "abstract": False, - "endpoints": [...], + "endpoints": [ + { + "ep_type": "slot", + "id": 0, + "label": "Alarme principale", + "name": "alarm1", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "slot", + "id": 1, + "label": "Alarme secondaire", + "name": "alarm2", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "slot", + "id": 2, + "label": "Zone temporisée", + "name": "timed", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 3, + "label": "Alarme principale", + "name": "alarm1", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 4, + "label": "Alarme secondaire", + "name": "alarm2", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 5, + "label": "Zone temporisée", + "name": "timed", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 6, + "label": "Détection", + "name": "trigger", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 7, + "label": "Couvercle", + "name": "1cover", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 8, + "label": "Niveau de Batterie", + "name": "1battery", + "param_type": "void", + "value_type": "int", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 9, + "label": "Batterie faible", + "name": "battery_warning", + "param_type": "void", + "value_type": "int", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 10, + "label": "Alarme", + "name": "alarm", + "param_type": "void", + "value_type": "void", + "visibility": "internal", + }, + ], "generic": False, "icon": "/resources/images/home/pictos/detecteur_ouverture.png", "inherit": "node::domus", @@ -2112,22 +2269,172 @@ DATA_HOME_GET_NODES = [ "ItemId": "240d000f9fefe576", }, "show_endpoints": [ - {...}, - {...}, - {...}, - {...}, - {...}, - {...}, - {...}, - {...}, - {...}, + { + "category": "", + "ep_type": "slot", + "id": 0, + "label": "Alarme principale", + "name": "alarm1", + "ui": {"access": "w", "display": "toggle"}, + "value": False, + "value_type": "bool", + "visibility": "normal", + }, + { + "category": "", + "ep_type": "slot", + "id": 1, + "label": "Alarme secondaire", + "name": "alarm2", + "ui": {"access": "w", "display": "toggle"}, + "value": False, + "value_type": "bool", + "visibility": "normal", + }, + { + "category": "", + "ep_type": "slot", + "id": 2, + "label": "Zone temporisée", + "name": "timed", + "ui": {"access": "w", "display": "toggle"}, + "value": False, + "value_type": "bool", + "visibility": "normal", + }, + { + "category": "", + "ep_type": "signal", + "id": 8, + "label": "Niveau de Batterie", + "name": "battery", + "refresh": 2000, + "ui": { + "access": "r", + "display": "icon", + "icon_range": [...], + "icon_url": "/resources/images/home/pictos/batt_x.png", + "status_text_range": [...], + "unit": "%", + }, + "value": 100, + "value_type": "int", + "visibility": "normal", + }, + ], + "signal_links": [ + { + "adapter": 5, + "category": "alarm", + "id": 7, + "label": "Système d alarme", + "link_id": 12, + "name": "node_7", + "status": "active", + } ], - "signal_links": [{...}], "slot_links": [], "status": "active", "type": { "abstract": False, - "endpoints": [...], + "endpoints": [ + { + "ep_type": "slot", + "id": 0, + "label": "Alarme principale", + "name": "alarm1", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "slot", + "id": 1, + "label": "Alarme secondaire", + "name": "alarm2", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "slot", + "id": 2, + "label": "Zone temporisée", + "name": "timed", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 3, + "label": "Alarme principale", + "name": "alarm1", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 4, + "label": "Alarme secondaire", + "name": "alarm2", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 5, + "label": "Zone temporisée", + "name": "timed", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 6, + "label": "Détection", + "name": "trigger", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 7, + "label": "Couvercle", + "name": "cover", + "param_type": "void", + "value_type": "bool", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 8, + "label": "Niveau de Batterie", + "name": "battery", + "param_type": "void", + "value_type": "int", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 9, + "label": "Batterie faible", + "name": "battery_warning", + "param_type": "void", + "value_type": "int", + "visibility": "normal", + }, + { + "ep_type": "signal", + "id": 10, + "label": "Alarme", + "name": "alarm", + "param_type": "void", + "value_type": "void", + "visibility": "internal", + }, + ], "generic": False, "icon": "/resources/images/home/pictos/detecteur_xxxx.png", "inherit": "node::domus",