Add energy binary sensors to Tessie (#121349)

This commit is contained in:
Brett Adams 2024-07-06 19:05:52 +10:00 committed by GitHub
parent f29094f41d
commit 696ae91c35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 221 additions and 8 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from itertools import chain
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -16,8 +17,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TessieConfigEntry from . import TessieConfigEntry
from .const import TessieState from .const import TessieState
from .entity import TessieEntity from .entity import TessieEnergyEntity, TessieEntity
from .models import TessieVehicleData from .models import TessieEnergyData, TessieVehicleData
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
@ -27,7 +28,7 @@ class TessieBinarySensorEntityDescription(BinarySensorEntityDescription):
is_on: Callable[..., bool] = lambda x: x is_on: Callable[..., bool] = lambda x: x
DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = ( VEHICLE_DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = (
TessieBinarySensorEntityDescription( TessieBinarySensorEntityDescription(
key="state", key="state",
device_class=BinarySensorDeviceClass.CONNECTIVITY, device_class=BinarySensorDeviceClass.CONNECTIVITY,
@ -157,6 +158,18 @@ DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = (
), ),
) )
ENERGY_LIVE_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(key="backup_capable"),
BinarySensorEntityDescription(key="grid_services_active"),
)
ENERGY_INFO_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="components_grid_services_enabled",
),
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -164,12 +177,24 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Tessie binary sensor platform from a config entry.""" """Set up the Tessie binary sensor platform from a config entry."""
data = entry.runtime_data
async_add_entities( async_add_entities(
TessieBinarySensorEntity(vehicle, description) chain(
for vehicle in data.vehicles (
for description in DESCRIPTIONS TessieBinarySensorEntity(vehicle, description)
for vehicle in entry.runtime_data.vehicles
for description in VEHICLE_DESCRIPTIONS
),
(
TessieEnergyLiveBinarySensorEntity(energy, description)
for energy in entry.runtime_data.energysites
for description in ENERGY_LIVE_DESCRIPTIONS
),
(
TessieEnergyInfoBinarySensorEntity(vehicle, description)
for vehicle in entry.runtime_data.energysites
for description in ENERGY_INFO_DESCRIPTIONS
),
)
) )
@ -191,3 +216,41 @@ class TessieBinarySensorEntity(TessieEntity, BinarySensorEntity):
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return the state of the binary sensor.""" """Return the state of the binary sensor."""
return self.entity_description.is_on(self._value) return self.entity_description.is_on(self._value)
class TessieEnergyLiveBinarySensorEntity(TessieEnergyEntity, BinarySensorEntity):
"""Base class for Tessie energy live binary sensors."""
entity_description: BinarySensorEntityDescription
def __init__(
self,
data: TessieEnergyData,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor."""
self.entity_description = description
super().__init__(data, data.live_coordinator, description.key)
def _async_update_attrs(self) -> None:
"""Update the attributes of the binary sensor."""
self._attr_is_on = self._value
class TessieEnergyInfoBinarySensorEntity(TessieEnergyEntity, BinarySensorEntity):
"""Base class for Tessie energy info binary sensors."""
entity_description: BinarySensorEntityDescription
def __init__(
self,
data: TessieEnergyData,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor."""
self.entity_description = description
super().__init__(data, data.info_coordinator, description.key)
def _async_update_attrs(self) -> None:
"""Update the attributes of the binary sensor."""
self._attr_is_on = self._value

View File

@ -22,6 +22,9 @@
"climate_state_auto_steering_wheel_heat": { "climate_state_auto_steering_wheel_heat": {
"default": "mdi:steering" "default": "mdi:steering"
}, },
"grid_services_power": {
"default": "mdi:transmission-tower"
},
"vehicle_state_dashcam_state": { "vehicle_state_dashcam_state": {
"default": "mdi:camera-off", "default": "mdi:camera-off",
"state": { "state": {

View File

@ -303,6 +303,9 @@
} }
}, },
"binary_sensor": { "binary_sensor": {
"backup_capable": {
"name": "Backup capable"
},
"state": { "state": {
"name": "Status" "name": "Status"
}, },
@ -345,6 +348,12 @@
"climate_state_cabin_overheat_protection_actively_cooling": { "climate_state_cabin_overheat_protection_actively_cooling": {
"name": "Cabin overheat protection actively cooling" "name": "Cabin overheat protection actively cooling"
}, },
"components_grid_services_enabled": {
"name": "Grid services enabled"
},
"grid_services_active": {
"name": "Grid services active"
},
"vehicle_state_dashcam_state": { "vehicle_state_dashcam_state": {
"name": "Dashcam" "name": "Dashcam"
}, },

View File

@ -1,4 +1,142 @@
# serializer version: 1 # serializer version: 1
# name: test_binary_sensors[binary_sensor.energy_site_backup_capable-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.energy_site_backup_capable',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Backup capable',
'platform': 'tessie',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'backup_capable',
'unique_id': '123456-backup_capable',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.energy_site_backup_capable-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Energy Site Backup capable',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.energy_site_backup_capable',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_binary_sensors[binary_sensor.energy_site_grid_services_active-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.energy_site_grid_services_active',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Grid services active',
'platform': 'tessie',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'grid_services_active',
'unique_id': '123456-grid_services_active',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.energy_site_grid_services_active-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Energy Site Grid services active',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.energy_site_grid_services_active',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.energy_site_grid_services_enabled-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.energy_site_grid_services_enabled',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Grid services enabled',
'platform': 'tessie',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'components_grid_services_enabled',
'unique_id': '123456-components_grid_services_enabled',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.energy_site_grid_services_enabled-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Energy Site Grid services enabled',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.energy_site_grid_services_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.test_auto_seat_climate_left-entry] # name: test_binary_sensors[binary_sensor.test_auto_seat_climate_left-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({