From 333711d9517c3c8cb09fa287f184ad50d0b60388 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 4 Jan 2024 09:57:00 +0100 Subject: [PATCH] Add sensor tests to Streamlabs water (#107065) --- .../components/streamlabswater/sensor.py | 6 + tests/components/streamlabswater/__init__.py | 14 ++ tests/components/streamlabswater/conftest.py | 35 +++++ .../fixtures/get_locations.json | 24 ++++ .../streamlabswater/fixtures/water_usage.json | 6 + .../snapshots/test_sensor.ambr | 136 ++++++++++++++++++ .../components/streamlabswater/test_sensor.py | 33 +++++ 7 files changed, 254 insertions(+) create mode 100644 tests/components/streamlabswater/fixtures/get_locations.json create mode 100644 tests/components/streamlabswater/fixtures/water_usage.json create mode 100644 tests/components/streamlabswater/snapshots/test_sensor.ambr create mode 100644 tests/components/streamlabswater/test_sensor.py diff --git a/homeassistant/components/streamlabswater/sensor.py b/homeassistant/components/streamlabswater/sensor.py index 6c869a6d1bc..6f17c23b4f5 100644 --- a/homeassistant/components/streamlabswater/sensor.py +++ b/homeassistant/components/streamlabswater/sensor.py @@ -44,11 +44,13 @@ class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntit _attr_device_class = SensorDeviceClass.WATER _attr_native_unit_of_measurement = UnitOfVolume.GALLONS + _key = "daily_usage" def __init__(self, coordinator: StreamlabsCoordinator, location_id: str) -> None: """Initialize the daily water usage device.""" super().__init__(coordinator) self._location_id = location_id + self._attr_unique_id = f"{location_id}-{self._key}" @property def location_data(self) -> StreamlabsData: @@ -69,6 +71,8 @@ class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntit class StreamLabsMonthlyUsage(StreamLabsDailyUsage): """Monitors the monthly water usage.""" + _key = "monthly_usage" + @property def name(self) -> str: """Return the name for monthly usage.""" @@ -83,6 +87,8 @@ class StreamLabsMonthlyUsage(StreamLabsDailyUsage): class StreamLabsYearlyUsage(StreamLabsDailyUsage): """Monitors the yearly water usage.""" + _key = "yearly_usage" + @property def name(self) -> str: """Return the name for yearly usage.""" diff --git a/tests/components/streamlabswater/__init__.py b/tests/components/streamlabswater/__init__.py index 16b2e5f0974..a467c9553de 100644 --- a/tests/components/streamlabswater/__init__.py +++ b/tests/components/streamlabswater/__init__.py @@ -1 +1,15 @@ """Tests for the StreamLabs integration.""" +from homeassistant.core import HomeAssistant +from homeassistant.util.unit_system import IMPERIAL_SYSTEM + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + hass.config.units = IMPERIAL_SYSTEM + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/streamlabswater/conftest.py b/tests/components/streamlabswater/conftest.py index f871332e5f6..64fbed63520 100644 --- a/tests/components/streamlabswater/conftest.py +++ b/tests/components/streamlabswater/conftest.py @@ -3,6 +3,12 @@ from collections.abc import Generator from unittest.mock import AsyncMock, patch import pytest +from streamlabswater.streamlabswater import StreamlabsClient + +from homeassistant.components.streamlabswater import DOMAIN +from homeassistant.const import CONF_API_KEY + +from tests.common import MockConfigEntry, load_json_object_fixture @pytest.fixture @@ -12,3 +18,32 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]: "homeassistant.components.streamlabswater.async_setup_entry", return_value=True ) as mock_setup_entry: yield mock_setup_entry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Create a mock StreamLabs config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title="StreamLabs", + data={CONF_API_KEY: "abc"}, + ) + + +@pytest.fixture(name="streamlabswater") +def mock_streamlabswater() -> Generator[AsyncMock, None, None]: + """Mock the StreamLabs client.""" + + locations = load_json_object_fixture("streamlabswater/get_locations.json") + + water_usage = load_json_object_fixture("streamlabswater/water_usage.json") + + mock = AsyncMock(spec=StreamlabsClient) + mock.get_locations.return_value = locations + mock.get_water_usage_summary.return_value = water_usage + + with patch( + "homeassistant.components.streamlabswater.StreamlabsClient", + return_value=mock, + ) as mock_client: + yield mock_client diff --git a/tests/components/streamlabswater/fixtures/get_locations.json b/tests/components/streamlabswater/fixtures/get_locations.json new file mode 100644 index 00000000000..bdf4deb1d1b --- /dev/null +++ b/tests/components/streamlabswater/fixtures/get_locations.json @@ -0,0 +1,24 @@ +{ + "pageCount": 1, + "perPage": 50, + "page": 1, + "total": 1, + "locations": [ + { + "locationId": "945e7c52-854a-41e1-8524-50c6993277e1", + "name": "Water Monitor", + "homeAway": "home", + "devices": [ + { + "deviceId": "09bec87a-fff2-4b8a-bc00-86d5928f19f3", + "type": "monitor", + "calibrated": true, + "connected": true + } + ], + "alerts": [], + "subscriptionIds": [], + "active": true + } + ] +} diff --git a/tests/components/streamlabswater/fixtures/water_usage.json b/tests/components/streamlabswater/fixtures/water_usage.json new file mode 100644 index 00000000000..1e902371f73 --- /dev/null +++ b/tests/components/streamlabswater/fixtures/water_usage.json @@ -0,0 +1,6 @@ +{ + "thisYear": 65432.389256934, + "today": 200.44691536, + "units": "gallons", + "thisMonth": 420.514099294 +} diff --git a/tests/components/streamlabswater/snapshots/test_sensor.ambr b/tests/components/streamlabswater/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..9a4c1cdc97e --- /dev/null +++ b/tests/components/streamlabswater/snapshots/test_sensor.ambr @@ -0,0 +1,136 @@ +# serializer version: 1 +# name: test_all_entities[sensor.water_monitor_daily_water-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.water_monitor_daily_water', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Water Monitor Daily Water', + 'platform': 'streamlabswater', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-daily_usage', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.water_monitor_daily_water-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'water', + 'friendly_name': 'Water Monitor Daily Water', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.water_monitor_daily_water', + 'last_changed': , + 'last_updated': , + 'state': '200.4', + }) +# --- +# name: test_all_entities[sensor.water_monitor_monthly_water-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.water_monitor_monthly_water', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Water Monitor Monthly Water', + 'platform': 'streamlabswater', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-monthly_usage', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.water_monitor_monthly_water-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'water', + 'friendly_name': 'Water Monitor Monthly Water', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.water_monitor_monthly_water', + 'last_changed': , + 'last_updated': , + 'state': '420.5', + }) +# --- +# name: test_all_entities[sensor.water_monitor_yearly_water-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.water_monitor_yearly_water', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Water Monitor Yearly Water', + 'platform': 'streamlabswater', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-yearly_usage', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.water_monitor_yearly_water-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'water', + 'friendly_name': 'Water Monitor Yearly Water', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.water_monitor_yearly_water', + 'last_changed': , + 'last_updated': , + 'state': '65432.4', + }) +# --- diff --git a/tests/components/streamlabswater/test_sensor.py b/tests/components/streamlabswater/test_sensor.py new file mode 100644 index 00000000000..a78d4129abb --- /dev/null +++ b/tests/components/streamlabswater/test_sensor.py @@ -0,0 +1,33 @@ +"""Tests for the Streamlabs Water sensor platform.""" +from unittest.mock import AsyncMock, patch + +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry +from tests.components.streamlabswater import setup_integration + + +async def test_all_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + streamlabswater: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.streamlabswater.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + entity_entries = er.async_entries_for_config_entry( + entity_registry, mock_config_entry.entry_id + ) + + assert entity_entries + for entity_entry in entity_entries: + assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry") + assert hass.states.get(entity_entry.entity_id) == snapshot( + name=f"{entity_entry.entity_id}-state" + )