Move lifespan attributes into own sensors for legacy Ecovacs bots (#122740)

* move available property to base entity class

* add lifespan sensors

* apply suggestion, simplify the method

* don't touch internals in tests

* apply suggestion

* apply suggestions
This commit is contained in:
Michael
2024-07-31 15:36:57 +02:00
committed by GitHub
parent a35fa0e95a
commit f7f0f49015
9 changed files with 294 additions and 12 deletions
+6 -5
View File
@@ -10,6 +10,7 @@ from deebot_client.device import Device
from deebot_client.exceptions import ApiError
from deebot_client.models import Credentials
import pytest
from sucks import EventEmitter
from homeassistant.components.ecovacs import PLATFORMS
from homeassistant.components.ecovacs.const import DOMAIN
@@ -128,10 +129,10 @@ def mock_vacbot(device_fixture: str) -> Generator[Mock]:
vacbot.vacuum = load_json_object_fixture(
f"devices/{device_fixture}/device.json", DOMAIN
)
vacbot.statusEvents = Mock()
vacbot.batteryEvents = Mock()
vacbot.lifespanEvents = Mock()
vacbot.errorEvents = Mock()
vacbot.statusEvents = EventEmitter()
vacbot.batteryEvents = EventEmitter()
vacbot.lifespanEvents = EventEmitter()
vacbot.errorEvents = EventEmitter()
vacbot.battery_status = None
vacbot.fan_speed = None
vacbot.components = {}
@@ -175,7 +176,7 @@ async def init_integration(
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
yield mock_config_entry
@@ -1,4 +1,152 @@
# serializer version: 1
# name: test_legacy_sensors[123][sensor.e1234567890000000003_filter_lifespan:entity-registry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.e1234567890000000003_filter_lifespan',
'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': 'Filter lifespan',
'platform': 'ecovacs',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'lifespan_filter',
'unique_id': 'E1234567890000000003_lifespan_filter',
'unit_of_measurement': '%',
})
# ---
# name: test_legacy_sensors[123][sensor.e1234567890000000003_filter_lifespan:state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'E1234567890000000003 Filter lifespan',
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.e1234567890000000003_filter_lifespan',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '40',
})
# ---
# name: test_legacy_sensors[123][sensor.e1234567890000000003_main_brush_lifespan:entity-registry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.e1234567890000000003_main_brush_lifespan',
'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': 'Main brush lifespan',
'platform': 'ecovacs',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'lifespan_main_brush',
'unique_id': 'E1234567890000000003_lifespan_main_brush',
'unit_of_measurement': '%',
})
# ---
# name: test_legacy_sensors[123][sensor.e1234567890000000003_main_brush_lifespan:state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'E1234567890000000003 Main brush lifespan',
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.e1234567890000000003_main_brush_lifespan',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '80',
})
# ---
# name: test_legacy_sensors[123][sensor.e1234567890000000003_side_brush_lifespan:entity-registry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.e1234567890000000003_side_brush_lifespan',
'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': 'Side brush lifespan',
'platform': 'ecovacs',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'lifespan_side_brush',
'unique_id': 'E1234567890000000003_lifespan_side_brush',
'unit_of_measurement': '%',
})
# ---
# name: test_legacy_sensors[123][sensor.e1234567890000000003_side_brush_lifespan:state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'E1234567890000000003 Side brush lifespan',
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.e1234567890000000003_side_brush_lifespan',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '60',
})
# ---
# name: test_legacy_sensors[123][states]
list([
'sensor.e1234567890000000003_main_brush_lifespan',
'sensor.e1234567890000000003_side_brush_lifespan',
'sensor.e1234567890000000003_filter_lifespan',
])
# ---
# name: test_sensors[5xu9h3][sensor.goat_g1_area_cleaned:entity-registry]
EntityRegistryEntrySnapshot({
'aliases': set({
+33
View File
@@ -1,5 +1,7 @@
"""Tests for Ecovacs sensors."""
from unittest.mock import Mock
from deebot_client.event_bus import EventBus
from deebot_client.events import (
BatteryEvent,
@@ -152,3 +154,34 @@ async def test_disabled_by_default_sensors(
), f"Entity registry entry for {entity_id} is missing"
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
@pytest.mark.usefixtures(
"entity_registry_enabled_by_default", "mock_vacbot", "init_integration"
)
@pytest.mark.parametrize(("device_fixture"), ["123"])
async def test_legacy_sensors(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_vacbot: Mock,
) -> None:
"""Test that sensor entity snapshots match."""
mock_vacbot.components = {"main_brush": 0.8, "side_brush": 0.6, "filter": 0.4}
mock_vacbot.lifespanEvents.notify("dummy_data")
await hass.async_block_till_done(wait_background_tasks=True)
states = hass.states.async_entity_ids()
assert snapshot(name="states") == states
for entity_id in hass.states.async_entity_ids():
assert (state := hass.states.get(entity_id)), f"State of {entity_id} is missing"
assert snapshot(name=f"{entity_id}:state") == state
assert (entity_entry := entity_registry.async_get(state.entity_id))
assert snapshot(name=f"{entity_id}:entity-registry") == entity_entry
assert entity_entry.device_id
assert (device_entry := device_registry.async_get(entity_entry.device_id))
assert device_entry.identifiers == {(DOMAIN, "E1234567890000000003")}