From 80042aa108b10b81d73df65ec911a1a394fe9353 Mon Sep 17 00:00:00 2001 From: Diogo Gomes <dgomes@ua.pt> Date: Sun, 12 Nov 2023 18:35:37 +0000 Subject: [PATCH] Add binary sensors to V2C (#103722) * Add binary_sensor to V2C * sort platforms * change to generator * make it generator * fix --- .coveragerc | 1 + homeassistant/components/v2c/__init__.py | 7 +- homeassistant/components/v2c/binary_sensor.py | 90 +++++++++++++++++++ homeassistant/components/v2c/strings.json | 11 +++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/v2c/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 4aa8ee4949e..7bec02cc47f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1433,6 +1433,7 @@ omit = homeassistant/components/upnp/sensor.py homeassistant/components/vasttrafik/sensor.py homeassistant/components/v2c/__init__.py + homeassistant/components/v2c/binary_sensor.py homeassistant/components/v2c/coordinator.py homeassistant/components/v2c/entity.py homeassistant/components/v2c/number.py diff --git a/homeassistant/components/v2c/__init__.py b/homeassistant/components/v2c/__init__.py index 97978a9ebc2..3cf615caa3c 100644 --- a/homeassistant/components/v2c/__init__.py +++ b/homeassistant/components/v2c/__init__.py @@ -11,7 +11,12 @@ from homeassistant.helpers.httpx_client import get_async_client from .const import DOMAIN from .coordinator import V2CUpdateCoordinator -PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH] +PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, + Platform.SENSOR, + Platform.SWITCH, + Platform.NUMBER, +] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/v2c/binary_sensor.py b/homeassistant/components/v2c/binary_sensor.py new file mode 100644 index 00000000000..7776a3398c7 --- /dev/null +++ b/homeassistant/components/v2c/binary_sensor.py @@ -0,0 +1,90 @@ +"""Support for V2C binary sensors.""" +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from pytrydan import Trydan + +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 .const import DOMAIN +from .coordinator import V2CUpdateCoordinator +from .entity import V2CBaseEntity + + +@dataclass +class V2CRequiredKeysMixin: + """Mixin for required keys.""" + + value_fn: Callable[[Trydan], bool] + + +@dataclass +class V2CBinarySensorEntityDescription( + BinarySensorEntityDescription, V2CRequiredKeysMixin +): + """Describes an EVSE binary sensor entity.""" + + +TRYDAN_SENSORS = ( + V2CBinarySensorEntityDescription( + key="connected", + translation_key="connected", + device_class=BinarySensorDeviceClass.PLUG, + value_fn=lambda evse: evse.connected, + ), + V2CBinarySensorEntityDescription( + key="charging", + translation_key="charging", + device_class=BinarySensorDeviceClass.BATTERY_CHARGING, + value_fn=lambda evse: evse.charging, + ), + V2CBinarySensorEntityDescription( + key="ready", + translation_key="ready", + value_fn=lambda evse: evse.ready, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up V2C binary sensor platform.""" + coordinator: V2CUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] + + async_add_entities( + V2CBinarySensorBaseEntity(coordinator, description, config_entry.entry_id) + for description in TRYDAN_SENSORS + ) + + +class V2CBinarySensorBaseEntity(V2CBaseEntity, BinarySensorEntity): + """Defines a base V2C binary_sensor entity.""" + + entity_description: V2CBinarySensorEntityDescription + + def __init__( + self, + coordinator: V2CUpdateCoordinator, + description: V2CBinarySensorEntityDescription, + entry_id: str, + ) -> None: + """Init the V2C base entity.""" + super().__init__(coordinator, description) + self._attr_unique_id = f"{entry_id}_{description.key}" + + @property + def is_on(self) -> bool: + """Return the state of the V2C binary_sensor.""" + return self.entity_description.value_fn(self.coordinator.evse) diff --git a/homeassistant/components/v2c/strings.json b/homeassistant/components/v2c/strings.json index 749cfb9979e..a0cf3aae03a 100644 --- a/homeassistant/components/v2c/strings.json +++ b/homeassistant/components/v2c/strings.json @@ -13,6 +13,17 @@ } }, "entity": { + "binary_sensor": { + "connected": { + "name": "Connected" + }, + "charging": { + "name": "Charging" + }, + "ready": { + "name": "Ready" + } + }, "number": { "intensity": { "name": "Intensity" -- GitLab