Add sensor tests to Streamlabs water (#107065)
This commit is contained in:
parent
254abeeb4f
commit
333711d951
7 changed files with 254 additions and 0 deletions
|
@ -44,11 +44,13 @@ class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntit
|
||||||
|
|
||||||
_attr_device_class = SensorDeviceClass.WATER
|
_attr_device_class = SensorDeviceClass.WATER
|
||||||
_attr_native_unit_of_measurement = UnitOfVolume.GALLONS
|
_attr_native_unit_of_measurement = UnitOfVolume.GALLONS
|
||||||
|
_key = "daily_usage"
|
||||||
|
|
||||||
def __init__(self, coordinator: StreamlabsCoordinator, location_id: str) -> None:
|
def __init__(self, coordinator: StreamlabsCoordinator, location_id: str) -> None:
|
||||||
"""Initialize the daily water usage device."""
|
"""Initialize the daily water usage device."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._location_id = location_id
|
self._location_id = location_id
|
||||||
|
self._attr_unique_id = f"{location_id}-{self._key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def location_data(self) -> StreamlabsData:
|
def location_data(self) -> StreamlabsData:
|
||||||
|
@ -69,6 +71,8 @@ class StreamLabsDailyUsage(CoordinatorEntity[StreamlabsCoordinator], SensorEntit
|
||||||
class StreamLabsMonthlyUsage(StreamLabsDailyUsage):
|
class StreamLabsMonthlyUsage(StreamLabsDailyUsage):
|
||||||
"""Monitors the monthly water usage."""
|
"""Monitors the monthly water usage."""
|
||||||
|
|
||||||
|
_key = "monthly_usage"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name for monthly usage."""
|
"""Return the name for monthly usage."""
|
||||||
|
@ -83,6 +87,8 @@ class StreamLabsMonthlyUsage(StreamLabsDailyUsage):
|
||||||
class StreamLabsYearlyUsage(StreamLabsDailyUsage):
|
class StreamLabsYearlyUsage(StreamLabsDailyUsage):
|
||||||
"""Monitors the yearly water usage."""
|
"""Monitors the yearly water usage."""
|
||||||
|
|
||||||
|
_key = "yearly_usage"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name for yearly usage."""
|
"""Return the name for yearly usage."""
|
||||||
|
|
|
@ -1 +1,15 @@
|
||||||
"""Tests for the StreamLabs integration."""
|
"""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()
|
||||||
|
|
|
@ -3,6 +3,12 @@ from collections.abc import Generator
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
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
|
@pytest.fixture
|
||||||
|
@ -12,3 +18,32 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||||
"homeassistant.components.streamlabswater.async_setup_entry", return_value=True
|
"homeassistant.components.streamlabswater.async_setup_entry", return_value=True
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry:
|
||||||
yield 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
|
||||||
|
|
24
tests/components/streamlabswater/fixtures/get_locations.json
Normal file
24
tests/components/streamlabswater/fixtures/get_locations.json
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"thisYear": 65432.389256934,
|
||||||
|
"today": 200.44691536,
|
||||||
|
"units": "gallons",
|
||||||
|
"thisMonth": 420.514099294
|
||||||
|
}
|
136
tests/components/streamlabswater/snapshots/test_sensor.ambr
Normal file
136
tests/components/streamlabswater/snapshots/test_sensor.ambr
Normal file
|
@ -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': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'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': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
|
||||||
|
'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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# 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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.water_monitor_daily_water',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '200.4',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_all_entities[sensor.water_monitor_monthly_water-entry]
|
||||||
|
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': None,
|
||||||
|
'entity_id': 'sensor.water_monitor_monthly_water',
|
||||||
|
'has_entity_name': False,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
|
||||||
|
'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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# 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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.water_monitor_monthly_water',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '420.5',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_all_entities[sensor.water_monitor_yearly_water-entry]
|
||||||
|
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': None,
|
||||||
|
'entity_id': 'sensor.water_monitor_yearly_water',
|
||||||
|
'has_entity_name': False,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.WATER: 'water'>,
|
||||||
|
'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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# 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': <UnitOfVolume.GALLONS: 'gal'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.water_monitor_yearly_water',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '65432.4',
|
||||||
|
})
|
||||||
|
# ---
|
33
tests/components/streamlabswater/test_sensor.py
Normal file
33
tests/components/streamlabswater/test_sensor.py
Normal file
|
@ -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"
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue