parent
cdc49328be
commit
07a46f17d0
|
@ -1,9 +1,21 @@
|
|||
"""Sanix tests configuration."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import pytest
|
||||
from sanix import (
|
||||
ATTR_API_BATTERY,
|
||||
ATTR_API_DEVICE_NO,
|
||||
ATTR_API_DISTANCE,
|
||||
ATTR_API_FILL_PERC,
|
||||
ATTR_API_SERVICE_DATE,
|
||||
ATTR_API_SSID,
|
||||
ATTR_API_STATUS,
|
||||
ATTR_API_TIME,
|
||||
)
|
||||
from sanix.models import Measurement
|
||||
|
||||
from homeassistant.components.sanix.const import CONF_SERIAL_NUMBER, DOMAIN
|
||||
|
@ -15,19 +27,31 @@ from tests.common import MockConfigEntry, load_json_object_fixture
|
|||
@pytest.fixture
|
||||
def mock_sanix():
|
||||
"""Build a fixture for the Sanix API that connects successfully and returns measurements."""
|
||||
fixture = load_json_object_fixture("sanix/get_measurements.json")
|
||||
mock_sanix_api = MagicMock()
|
||||
fixture = load_json_object_fixture("get_measurements.json", DOMAIN)
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.sanix.config_flow.Sanix",
|
||||
return_value=mock_sanix_api,
|
||||
autospec=True,
|
||||
) as mock_sanix_api,
|
||||
patch(
|
||||
"homeassistant.components.sanix.Sanix",
|
||||
return_value=mock_sanix_api,
|
||||
new=mock_sanix_api,
|
||||
),
|
||||
):
|
||||
mock_sanix_api.return_value.fetch_data.return_value = Measurement(**fixture)
|
||||
mock_sanix_api.return_value.fetch_data.return_value = Measurement(
|
||||
battery=fixture[ATTR_API_BATTERY],
|
||||
device_no=fixture[ATTR_API_DEVICE_NO],
|
||||
distance=fixture[ATTR_API_DISTANCE],
|
||||
fill_perc=fixture[ATTR_API_FILL_PERC],
|
||||
service_date=datetime.strptime(
|
||||
fixture[ATTR_API_SERVICE_DATE], "%d.%m.%Y"
|
||||
).date(),
|
||||
ssid=fixture[ATTR_API_SSID],
|
||||
status=fixture[ATTR_API_STATUS],
|
||||
time=datetime.strptime(fixture[ATTR_API_TIME], "%d.%m.%Y %H:%M:%S").replace(
|
||||
tzinfo=ZoneInfo("Europe/Warsaw")
|
||||
),
|
||||
)
|
||||
yield mock_sanix_api
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,292 @@
|
|||
# serializer version: 1
|
||||
# name: test_all_entities[sensor.sanix_battery-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.sanix_battery',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.BATTERY: 'battery'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Battery',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'battery',
|
||||
'unique_id': '1810088-battery',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_battery-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'battery',
|
||||
'friendly_name': 'Sanix Battery',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_battery',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '100',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_device_number-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.sanix_device_number',
|
||||
'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': 'Device number',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'device_no',
|
||||
'unique_id': '1810088-device_no',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_device_number-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Sanix Device number',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_device_number',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'SANIX-1810088',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_distance-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.sanix_distance',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DISTANCE: 'distance'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Distance',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'distance',
|
||||
'unique_id': '1810088-distance',
|
||||
'unit_of_measurement': <UnitOfLength.CENTIMETERS: 'cm'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_distance-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'distance',
|
||||
'friendly_name': 'Sanix Distance',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfLength.CENTIMETERS: 'cm'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_distance',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '109',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_filled-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.sanix_filled',
|
||||
'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': 'Filled',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'fill_perc',
|
||||
'unique_id': '1810088-fill_perc',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_filled-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Sanix Filled',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_filled',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '32',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_service_date-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.sanix_service_date',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.DATE: 'date'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Service date',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'service_date',
|
||||
'unique_id': '1810088-service_date',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_service_date-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'date',
|
||||
'friendly_name': 'Sanix Service date',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_service_date',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2024-06-15',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_ssid-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.sanix_ssid',
|
||||
'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': 'SSID',
|
||||
'platform': 'sanix',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'ssid',
|
||||
'unique_id': '1810088-ssid',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[sensor.sanix_ssid-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Sanix SSID',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.sanix_ssid',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'Wifi',
|
||||
})
|
||||
# ---
|
|
@ -1,4 +1,4 @@
|
|||
"""Test the Home Assistant analytics init module."""
|
||||
"""Test the Sanix init module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
"""Test the Sanix sensor module."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_sanix: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
with patch(
|
||||
"homeassistant.components.sanix.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 (state := hass.states.get(entity_entry.entity_id))
|
||||
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
Loading…
Reference in New Issue