From e328d3ec5e01048b55e7602b33c28c573bdca5f0 Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Fri, 2 Feb 2024 22:21:13 +1000 Subject: [PATCH] Add Charging sensor to Tessie (#108205) --- .../components/tessie/binary_sensor.py | 1 + homeassistant/components/tessie/const.py | 10 +++ homeassistant/components/tessie/sensor.py | 9 ++- homeassistant/components/tessie/strings.json | 11 ++++ .../tessie/snapshots/test_sensor.ambr | 62 +++++++++++++++++++ .../components/tessie/test_binary_sensors.py | 2 + 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/tessie/binary_sensor.py b/homeassistant/components/tessie/binary_sensor.py index 65bfd483f18..ff0cf661475 100644 --- a/homeassistant/components/tessie/binary_sensor.py +++ b/homeassistant/components/tessie/binary_sensor.py @@ -41,6 +41,7 @@ DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = ( key="charge_state_charging_state", device_class=BinarySensorDeviceClass.BATTERY_CHARGING, is_on=lambda x: x == "Charging", + entity_registry_enabled_default=False, ), TessieBinarySensorEntityDescription( key="charge_state_preconditioning_enabled", diff --git a/homeassistant/components/tessie/const.py b/homeassistant/components/tessie/const.py index 591d4652274..8ec063bf47c 100644 --- a/homeassistant/components/tessie/const.py +++ b/homeassistant/components/tessie/const.py @@ -68,3 +68,13 @@ class TessieChargeCableLockStates(StrEnum): ENGAGED = "Engaged" DISENGAGED = "Disengaged" + + +TessieChargeStates = { + "Starting": "starting", + "Charging": "charging", + "Stopped": "stopped", + "Complete": "complete", + "Disconnected": "disconnected", + "NoPower": "no_power", +} diff --git a/homeassistant/components/tessie/sensor.py b/homeassistant/components/tessie/sensor.py index 36896863120..7c126754fb5 100644 --- a/homeassistant/components/tessie/sensor.py +++ b/homeassistant/components/tessie/sensor.py @@ -32,7 +32,7 @@ from homeassistant.helpers.typing import StateType from homeassistant.util import dt as dt_util from homeassistant.util.variance import ignore_variance -from .const import DOMAIN +from .const import DOMAIN, TessieChargeStates from .coordinator import TessieStateUpdateCoordinator from .entity import TessieEntity @@ -54,6 +54,13 @@ class TessieSensorEntityDescription(SensorEntityDescription): DESCRIPTIONS: tuple[TessieSensorEntityDescription, ...] = ( + TessieSensorEntityDescription( + key="charge_state_charging_state", + icon="mdi:ev-station", + options=list(TessieChargeStates.values()), + device_class=SensorDeviceClass.ENUM, + value_fn=lambda value: TessieChargeStates[cast(str, value)], + ), TessieSensorEntityDescription( key="charge_state_usable_battery_level", state_class=SensorStateClass.MEASUREMENT, diff --git a/homeassistant/components/tessie/strings.json b/homeassistant/components/tessie/strings.json index 381a5e3d4c0..01e6a654163 100644 --- a/homeassistant/components/tessie/strings.json +++ b/homeassistant/components/tessie/strings.json @@ -67,6 +67,17 @@ } }, "sensor": { + "charge_state_charging_state": { + "name": "Charging", + "state": { + "starting": "Starting", + "charging": "Charging", + "disconnected": "Disconnected", + "stopped": "Stopped", + "complete": "Complete", + "no_power": "No power" + } + }, "charge_state_usable_battery_level": { "name": "Battery level" }, diff --git a/tests/components/tessie/snapshots/test_sensor.ambr b/tests/components/tessie/snapshots/test_sensor.ambr index 0c01fc50244..b9a423bfa9a 100644 --- a/tests/components/tessie/snapshots/test_sensor.ambr +++ b/tests/components/tessie/snapshots/test_sensor.ambr @@ -347,6 +347,68 @@ 'state': '224', }) # --- +# name: test_sensors[sensor.test_charging-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'starting', + 'charging', + 'stopped', + 'complete', + 'disconnected', + 'no_power', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_charging', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:ev-station', + 'original_name': 'Charging', + 'platform': 'tessie', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'charge_state_charging_state', + 'unique_id': 'VINVINVIN-charge_state_charging_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.test_charging-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'Test Charging', + 'icon': 'mdi:ev-station', + 'options': list([ + 'starting', + 'charging', + 'stopped', + 'complete', + 'disconnected', + 'no_power', + ]), + }), + 'context': , + 'entity_id': 'sensor.test_charging', + 'last_changed': , + 'last_updated': , + 'state': 'charging', + }) +# --- # name: test_sensors[sensor.test_destination-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/tessie/test_binary_sensors.py b/tests/components/tessie/test_binary_sensors.py index ca53a60d493..b6dccd9d3b1 100644 --- a/tests/components/tessie/test_binary_sensors.py +++ b/tests/components/tessie/test_binary_sensors.py @@ -1,4 +1,5 @@ """Test the Tessie binary sensor platform.""" +import pytest from syrupy import SnapshotAssertion from homeassistant.const import Platform @@ -8,6 +9,7 @@ from homeassistant.helpers import entity_registry as er from .common import assert_entities, setup_platform +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_binary_sensors( hass: HomeAssistant, snapshot: SnapshotAssertion, entity_registry: er.EntityRegistry ) -> None: