-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce base entity in streamlabs water (#107095)
- Loading branch information
Showing
6 changed files
with
126 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Base entity for Streamlabs integration.""" | ||
from homeassistant.core import DOMAIN | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
||
from .coordinator import StreamlabsCoordinator, StreamlabsData | ||
|
||
|
||
class StreamlabsWaterEntity(CoordinatorEntity[StreamlabsCoordinator]): | ||
"""Defines a base Streamlabs entity.""" | ||
|
||
_attr_has_entity_name = True | ||
|
||
def __init__( | ||
self, | ||
coordinator: StreamlabsCoordinator, | ||
location_id: str, | ||
key: str, | ||
) -> None: | ||
"""Initialize the Streamlabs entity.""" | ||
super().__init__(coordinator) | ||
self._location_id = location_id | ||
self._attr_unique_id = f"{location_id}-{key}" | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, location_id)}, name=self.location_data.name | ||
) | ||
|
||
@property | ||
def location_data(self) -> StreamlabsData: | ||
"""Returns the data object.""" | ||
return self.coordinator.data[self._location_id] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/components/streamlabswater/snapshots/test_binary_sensor.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# serializer version: 1 | ||
# name: test_all_entities[binary_sensor.water_monitor_away_mode-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'binary_sensor', | ||
'entity_category': None, | ||
'entity_id': 'binary_sensor.water_monitor_away_mode', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': None, | ||
'original_icon': None, | ||
'original_name': 'Away mode', | ||
'platform': 'streamlabswater', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'away_mode', | ||
'unique_id': '945e7c52-854a-41e1-8524-50c6993277e1-away_mode', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_entities[binary_sensor.water_monitor_away_mode-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'friendly_name': 'Water Monitor Away mode', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'binary_sensor.water_monitor_away_mode', | ||
'last_changed': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'off', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Tests for the Streamlabs Water binary 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.BINARY_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" | ||
) |