Add binary sensor platform to Teslemetry (#117230)
* Add binary sensor platform * Add tests * Cleanup * Add refresh test * Fix runtime_data after rebase * Remove streaming strings * test error * updated_once * fix updated_once * assert_entities_alt * Update homeassistant/components/teslemetry/binary_sensor.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se>pull/118010/head^2
parent
8da799e420
commit
ad90ecef3f
|
@ -27,6 +27,7 @@ from .coordinator import (
|
|||
from .models import TeslemetryData, TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
PLATFORMS: Final = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.CLIMATE,
|
||||
Platform.LOCK,
|
||||
Platform.SELECT,
|
||||
|
|
|
@ -0,0 +1,271 @@
|
|||
"""Binary Sensor platform for Teslemetry integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from itertools import chain
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import TeslemetryState
|
||||
from .entity import (
|
||||
TeslemetryEnergyInfoEntity,
|
||||
TeslemetryEnergyLiveEntity,
|
||||
TeslemetryVehicleEntity,
|
||||
)
|
||||
from .models import TeslemetryEnergyData, TeslemetryVehicleData
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TeslemetryBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""Describes Teslemetry binary sensor entity."""
|
||||
|
||||
is_on: Callable[[StateType], bool] = bool
|
||||
|
||||
|
||||
VEHICLE_DESCRIPTIONS: tuple[TeslemetryBinarySensorEntityDescription, ...] = (
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="state",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
is_on=lambda x: x == TeslemetryState.ONLINE,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_battery_heater_on",
|
||||
device_class=BinarySensorDeviceClass.HEAT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_charger_phases",
|
||||
is_on=lambda x: cast(int, x) > 1,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_preconditioning_enabled",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="climate_state_is_preconditioning",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_scheduled_charging_pending",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_trip_charging",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="charge_state_conn_charge_cable",
|
||||
is_on=lambda x: x != "<invalid>",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="climate_state_cabin_overheat_protection_actively_cooling",
|
||||
device_class=BinarySensorDeviceClass.HEAT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_dashcam_state",
|
||||
device_class=BinarySensorDeviceClass.RUNNING,
|
||||
is_on=lambda x: x == "Recording",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_is_user_present",
|
||||
device_class=BinarySensorDeviceClass.PRESENCE,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_fl",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_fr",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_rl",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_rr",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_fd_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_fp_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_rd_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_rp_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_df",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_dr",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_pf",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslemetryBinarySensorEntityDescription(
|
||||
key="vehicle_state_pr",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
)
|
||||
|
||||
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(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Teslemetry binary sensor platform from a config entry."""
|
||||
|
||||
async_add_entities(
|
||||
chain(
|
||||
( # Vehicles
|
||||
TeslemetryVehicleBinarySensorEntity(vehicle, description)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in VEHICLE_DESCRIPTIONS
|
||||
),
|
||||
( # Energy Site Live
|
||||
TeslemetryEnergyLiveBinarySensorEntity(energysite, description)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_LIVE_DESCRIPTIONS
|
||||
if energysite.info_coordinator.data.get("components_battery")
|
||||
),
|
||||
( # Energy Site Info
|
||||
TeslemetryEnergyInfoBinarySensorEntity(energysite, description)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_INFO_DESCRIPTIONS
|
||||
if energysite.info_coordinator.data.get("components_battery")
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TeslemetryVehicleBinarySensorEntity(TeslemetryVehicleEntity, BinarySensorEntity):
|
||||
"""Base class for Teslemetry vehicle binary sensors."""
|
||||
|
||||
entity_description: TeslemetryBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryVehicleData,
|
||||
description: TeslemetryBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
|
||||
if self.coordinator.updated_once:
|
||||
if self._value is None:
|
||||
self._attr_available = False
|
||||
self._attr_is_on = None
|
||||
else:
|
||||
self._attr_available = True
|
||||
self._attr_is_on = self.entity_description.is_on(self._value)
|
||||
else:
|
||||
self._attr_is_on = None
|
||||
|
||||
|
||||
class TeslemetryEnergyLiveBinarySensorEntity(
|
||||
TeslemetryEnergyLiveEntity, BinarySensorEntity
|
||||
):
|
||||
"""Base class for Teslemetry energy live binary sensors."""
|
||||
|
||||
entity_description: BinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryEnergyData,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
self._attr_is_on = self._value
|
||||
|
||||
|
||||
class TeslemetryEnergyInfoBinarySensorEntity(
|
||||
TeslemetryEnergyInfoEntity, BinarySensorEntity
|
||||
):
|
||||
"""Base class for Teslemetry energy info binary sensors."""
|
||||
|
||||
entity_description: BinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslemetryEnergyData,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
self._attr_is_on = self._value
|
|
@ -48,7 +48,7 @@ def flatten(data: dict[str, Any], parent: str | None = None) -> dict[str, Any]:
|
|||
class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Class to manage fetching data from the Teslemetry API."""
|
||||
|
||||
name = "Teslemetry Vehicle"
|
||||
updated_once: bool
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, api: VehicleSpecific, product: dict
|
||||
|
@ -62,6 +62,7 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||
)
|
||||
self.api = api
|
||||
self.data = flatten(product)
|
||||
self.updated_once = False
|
||||
|
||||
async def _async_update_data(self) -> dict[str, Any]:
|
||||
"""Update vehicle data using Teslemetry API."""
|
||||
|
@ -77,12 +78,15 @@ class TeslemetryVehicleDataCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||
except TeslaFleetError as e:
|
||||
raise UpdateFailed(e.message) from e
|
||||
|
||||
self.updated_once = True
|
||||
return flatten(data)
|
||||
|
||||
|
||||
class TeslemetryEnergySiteLiveCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Class to manage fetching energy site live status from the Teslemetry API."""
|
||||
|
||||
updated_once: bool
|
||||
|
||||
def __init__(self, hass: HomeAssistant, api: EnergySpecific) -> None:
|
||||
"""Initialize Teslemetry Energy Site Live coordinator."""
|
||||
super().__init__(
|
||||
|
@ -116,6 +120,8 @@ class TeslemetryEnergySiteLiveCoordinator(DataUpdateCoordinator[dict[str, Any]])
|
|||
class TeslemetryEnergySiteInfoCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
"""Class to manage fetching energy site info from the Teslemetry API."""
|
||||
|
||||
updated_once: bool
|
||||
|
||||
def __init__(self, hass: HomeAssistant, api: EnergySpecific, product: dict) -> None:
|
||||
"""Initialize Teslemetry Energy Info coordinator."""
|
||||
super().__init__(
|
||||
|
|
|
@ -1,5 +1,43 @@
|
|||
{
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"climate_state_is_preconditioning": {
|
||||
"state": {
|
||||
"off": "mdi:hvac-off",
|
||||
"on": "mdi:hvac"
|
||||
}
|
||||
},
|
||||
"vehicle_state_is_user_present": {
|
||||
"state": {
|
||||
"off": "mdi:account-remove-outline",
|
||||
"on": "mdi:account"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fl": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fr": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rl": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rr": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
}
|
||||
},
|
||||
"climate": {
|
||||
"driver_temp": {
|
||||
"state_attributes": {
|
||||
|
|
|
@ -16,6 +16,86 @@
|
|||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"backup_capable": {
|
||||
"name": "Backup capable"
|
||||
},
|
||||
"charge_state_battery_heater_on": {
|
||||
"name": "Battery heater"
|
||||
},
|
||||
"charge_state_charger_phases": {
|
||||
"name": "Charger has multiple phases"
|
||||
},
|
||||
"charge_state_conn_charge_cable": {
|
||||
"name": "Charge cable"
|
||||
},
|
||||
"charge_state_preconditioning_enabled": {
|
||||
"name": "Preconditioning enabled"
|
||||
},
|
||||
"charge_state_scheduled_charging_pending": {
|
||||
"name": "Scheduled charging pending"
|
||||
},
|
||||
"charge_state_trip_charging": {
|
||||
"name": "Trip charging"
|
||||
},
|
||||
"climate_state_cabin_overheat_protection_actively_cooling": {
|
||||
"name": "Cabin overheat protection actively cooling"
|
||||
},
|
||||
"climate_state_is_preconditioning": {
|
||||
"name": "Preconditioning"
|
||||
},
|
||||
"components_grid_services_enabled": {
|
||||
"name": "Grid services enabled"
|
||||
},
|
||||
"grid_services_active": {
|
||||
"name": "Grid services active"
|
||||
},
|
||||
"state": {
|
||||
"name": "Status"
|
||||
},
|
||||
"vehicle_state_dashcam_state": {
|
||||
"name": "Dashcam"
|
||||
},
|
||||
"vehicle_state_df": {
|
||||
"name": "Front driver door"
|
||||
},
|
||||
"vehicle_state_dr": {
|
||||
"name": "Rear driver door"
|
||||
},
|
||||
"vehicle_state_fd_window": {
|
||||
"name": "Front driver window"
|
||||
},
|
||||
"vehicle_state_fp_window": {
|
||||
"name": "Front passenger window"
|
||||
},
|
||||
"vehicle_state_is_user_present": {
|
||||
"name": "User present"
|
||||
},
|
||||
"vehicle_state_pf": {
|
||||
"name": "Front passenger door"
|
||||
},
|
||||
"vehicle_state_pr": {
|
||||
"name": "Rear passenger door"
|
||||
},
|
||||
"vehicle_state_rd_window": {
|
||||
"name": "Rear driver window"
|
||||
},
|
||||
"vehicle_state_rp_window": {
|
||||
"name": "Rear passenger window"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fl": {
|
||||
"name": "Tire pressure warning front left"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fr": {
|
||||
"name": "Tire pressure warning front right"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rl": {
|
||||
"name": "Tire pressure warning rear left"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rr": {
|
||||
"name": "Tire pressure warning rear right"
|
||||
}
|
||||
},
|
||||
"climate": {
|
||||
"driver_temp": {
|
||||
"name": "[%key:component::climate::title%]",
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
"backseat_token_updated_at": null,
|
||||
"ble_autopair_enrolled": false,
|
||||
"charge_state": {
|
||||
"battery_heater_on": false,
|
||||
"battery_heater_on": true,
|
||||
"battery_level": 77,
|
||||
"battery_range": 266.87,
|
||||
"charge_amps": 16,
|
||||
|
@ -76,7 +76,7 @@
|
|||
"auto_seat_climate_left": false,
|
||||
"auto_seat_climate_right": false,
|
||||
"auto_steering_wheel_heat": false,
|
||||
"battery_heater": false,
|
||||
"battery_heater": true,
|
||||
"battery_heater_no_power": null,
|
||||
"cabin_overheat_protection": "Off",
|
||||
"cabin_overheat_protection_actively_cooling": false,
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,61 @@
|
|||
"""Test the Teslemetry binary sensor platform."""
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
from tesla_fleet_api.exceptions import VehicleOffline
|
||||
|
||||
from homeassistant.components.teslemetry.coordinator import VEHICLE_INTERVAL
|
||||
from homeassistant.const import STATE_UNKNOWN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import assert_entities, assert_entities_alt, setup_platform
|
||||
from .const import VEHICLE_DATA_ALT
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_binary_sensor(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct."""
|
||||
|
||||
entry = await setup_platform(hass, [Platform.BINARY_SENSOR])
|
||||
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_binary_sensor_refresh(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_vehicle_data,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct."""
|
||||
|
||||
entry = await setup_platform(hass, [Platform.BINARY_SENSOR])
|
||||
|
||||
# Refresh
|
||||
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
|
||||
freezer.tick(VEHICLE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert_entities_alt(hass, entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
async def test_binary_sensor_offline(
|
||||
hass: HomeAssistant,
|
||||
mock_vehicle_data,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct when offline."""
|
||||
|
||||
mock_vehicle_data.side_effect = VehicleOffline
|
||||
await setup_platform(hass, [Platform.BINARY_SENSOR])
|
||||
state = hass.states.get("binary_sensor.test_status")
|
||||
assert state.state == STATE_UNKNOWN
|
Loading…
Reference in New Issue