Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary sensors to V2C #103722

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/v2c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
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:
Expand Down
90 changes: 90 additions & 0 deletions homeassistant/components/v2c/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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",
joostlek marked this conversation as resolved.
Show resolved Hide resolved
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)
11 changes: 11 additions & 0 deletions homeassistant/components/v2c/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
}
},
"entity": {
"binary_sensor": {
"connected": {
"name": "Connected"
},
"charging": {
"name": "Charging"
},
"ready": {
"name": "Ready"
}
},
"number": {
"intensity": {
"name": "Intensity"
Expand Down