Cleanup tests for tedee (#133306)

This commit is contained in:
Josef Zweck 2024-12-15 20:44:28 +01:00 committed by GitHub
parent 5cc8d9e105
commit 89387760d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 567 additions and 221 deletions

View File

@ -1 +1,14 @@
"""Add tests for Tedee components.""" """Add tests for Tedee components."""
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def setup_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Set up the acaia integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

View File

@ -14,6 +14,8 @@ from homeassistant.components.tedee.const import CONF_LOCAL_ACCESS_TOKEN, DOMAIN
from homeassistant.const import CONF_HOST, CONF_WEBHOOK_ID from homeassistant.const import CONF_HOST, CONF_WEBHOOK_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import setup_integration
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
WEBHOOK_ID = "bq33efxmdi3vxy55q2wbnudbra7iv8mjrq9x0gea33g4zqtd87093pwveg8xcb33" WEBHOOK_ID = "bq33efxmdi3vxy55q2wbnudbra7iv8mjrq9x0gea33g4zqtd87093pwveg8xcb33"
@ -84,8 +86,6 @@ async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_tedee: MagicMock hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_tedee: MagicMock
) -> MockConfigEntry: ) -> MockConfigEntry:
"""Set up the Tedee integration for testing.""" """Set up the Tedee integration for testing."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry return mock_config_entry

View File

@ -1,5 +1,5 @@
# serializer version: 1 # serializer version: 1
# name: test_binary_sensors[entry-charging] # name: test_binary_sensors[binary_sensor.lock_1a2b_charging-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -32,7 +32,21 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_binary_sensors[entry-lock_uncalibrated] # name: test_binary_sensors[binary_sensor.lock_1a2b_charging-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery_charging',
'friendly_name': 'Lock-1A2B Charging',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_charging',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_1a2b_lock_uncalibrated-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -65,7 +79,21 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_binary_sensors[entry-pullspring_enabled] # name: test_binary_sensors[binary_sensor.lock_1a2b_lock_uncalibrated-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Lock-1A2B Lock uncalibrated',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_lock_uncalibrated',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_1a2b_pullspring_enabled-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -98,7 +126,20 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_binary_sensors[entry-semi_locked] # name: test_binary_sensors[binary_sensor.lock_1a2b_pullspring_enabled-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-1A2B Pullspring enabled',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_pullspring_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_1a2b_semi_locked-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -131,48 +172,7 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_binary_sensors[state-charging] # name: test_binary_sensors[binary_sensor.lock_1a2b_semi_locked-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery_charging',
'friendly_name': 'Lock-1A2B Charging',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_charging',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[state-lock_uncalibrated]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Lock-1A2B Lock uncalibrated',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_lock_uncalibrated',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[state-pullspring_enabled]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-1A2B Pullspring enabled',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_1a2b_pullspring_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_binary_sensors[state-semi_locked]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'friendly_name': 'Lock-1A2B Semi locked', 'friendly_name': 'Lock-1A2B Semi locked',
@ -185,3 +185,189 @@
'state': 'off', 'state': 'off',
}) })
# --- # ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_charging-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.lock_2c3d_charging',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.BATTERY_CHARGING: 'battery_charging'>,
'original_icon': None,
'original_name': 'Charging',
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '98765-charging',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_charging-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery_charging',
'friendly_name': 'Lock-2C3D Charging',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_2c3d_charging',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_lock_uncalibrated-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.lock_2c3d_lock_uncalibrated',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
'original_icon': None,
'original_name': 'Lock uncalibrated',
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'uncalibrated',
'unique_id': '98765-uncalibrated',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_lock_uncalibrated-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Lock-2C3D Lock uncalibrated',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_2c3d_lock_uncalibrated',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_pullspring_enabled-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.lock_2c3d_pullspring_enabled',
'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': 'Pullspring enabled',
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'pullspring_enabled',
'unique_id': '98765-pullspring_enabled',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_pullspring_enabled-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-2C3D Pullspring enabled',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_2c3d_pullspring_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_semi_locked-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.lock_2c3d_semi_locked',
'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': 'Semi locked',
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'semi_locked',
'unique_id': '98765-semi_locked',
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensors[binary_sensor.lock_2c3d_semi_locked-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-2C3D Semi locked',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.lock_2c3d_semi_locked',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -31,3 +31,35 @@
'via_device_id': None, 'via_device_id': None,
}) })
# --- # ---
# name: test_lock_device
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': None,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'tedee',
'12345',
),
}),
'is_new': False,
'labels': set({
}),
'manufacturer': 'Tedee',
'model': 'Tedee PRO',
'model_id': 'Tedee PRO',
'name': 'Lock-1A2B',
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': <ANY>,
})
# ---

View File

@ -1,83 +1,4 @@
# serializer version: 1 # serializer version: 1
# name: test_lock
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-1A2B',
'supported_features': <LockEntityFeature: 1>,
}),
'context': <ANY>,
'entity_id': 'lock.lock_1a2b',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unlocked',
})
# ---
# name: test_lock.1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'lock',
'entity_category': None,
'entity_id': 'lock.lock_1a2b',
'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': None,
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': <LockEntityFeature: 1>,
'translation_key': None,
'unique_id': '12345-lock',
'unit_of_measurement': None,
})
# ---
# name: test_lock.2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': None,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'tedee',
'12345',
),
}),
'is_new': False,
'labels': set({
}),
'manufacturer': 'Tedee',
'model': 'Tedee PRO',
'model_id': 'Tedee PRO',
'name': 'Lock-1A2B',
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': <ANY>,
})
# ---
# name: test_lock_without_pullspring # name: test_lock_without_pullspring
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
@ -157,3 +78,97 @@
'via_device_id': <ANY>, 'via_device_id': <ANY>,
}) })
# --- # ---
# name: test_locks[lock.lock_1a2b-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'lock',
'entity_category': None,
'entity_id': 'lock.lock_1a2b',
'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': None,
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': <LockEntityFeature: 1>,
'translation_key': None,
'unique_id': '12345-lock',
'unit_of_measurement': None,
})
# ---
# name: test_locks[lock.lock_1a2b-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-1A2B',
'supported_features': <LockEntityFeature: 1>,
}),
'context': <ANY>,
'entity_id': 'lock.lock_1a2b',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unlocked',
})
# ---
# name: test_locks[lock.lock_2c3d-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'lock',
'entity_category': None,
'entity_id': 'lock.lock_2c3d',
'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': None,
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '98765-lock',
'unit_of_measurement': None,
})
# ---
# name: test_locks[lock.lock_2c3d-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Lock-2C3D',
'supported_features': <LockEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'lock.lock_2c3d',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unlocked',
})
# ---

View File

@ -1,5 +1,5 @@
# serializer version: 1 # serializer version: 1
# name: test_sensors[entry-battery] # name: test_sensors[sensor.lock_1a2b_battery-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -34,7 +34,23 @@
'unit_of_measurement': '%', 'unit_of_measurement': '%',
}) })
# --- # ---
# name: test_sensors[entry-pullspring_duration] # name: test_sensors[sensor.lock_1a2b_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
'friendly_name': 'Lock-1A2B Battery',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.lock_1a2b_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '70',
})
# ---
# name: test_sensors[sensor.lock_1a2b_pullspring_duration-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -69,23 +85,7 @@
'unit_of_measurement': <UnitOfTime.SECONDS: 's'>, 'unit_of_measurement': <UnitOfTime.SECONDS: 's'>,
}) })
# --- # ---
# name: test_sensors[state-battery] # name: test_sensors[sensor.lock_1a2b_pullspring_duration-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
'friendly_name': 'Lock-1A2B Battery',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.lock_1a2b_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '70',
})
# ---
# name: test_sensors[state-pullspring_duration]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'device_class': 'duration', 'device_class': 'duration',
@ -101,3 +101,105 @@
'state': '2', 'state': '2',
}) })
# --- # ---
# name: test_sensors[sensor.lock_2c3d_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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.lock_2c3d_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': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '98765-battery_sensor',
'unit_of_measurement': '%',
})
# ---
# name: test_sensors[sensor.lock_2c3d_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
'friendly_name': 'Lock-2C3D Battery',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.lock_2c3d_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '70',
})
# ---
# name: test_sensors[sensor.lock_2c3d_pullspring_duration-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.lock_2c3d_pullspring_duration',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.DURATION: 'duration'>,
'original_icon': None,
'original_name': 'Pullspring duration',
'platform': 'tedee',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'pullspring_duration',
'unique_id': '98765-pullspring_duration',
'unit_of_measurement': <UnitOfTime.SECONDS: 's'>,
})
# ---
# name: test_sensors[sensor.lock_2c3d_pullspring_duration-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'duration',
'friendly_name': 'Lock-2C3D Pullspring duration',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfTime.SECONDS: 's'>,
}),
'context': <ANY>,
'entity_id': 'sensor.lock_2c3d_pullspring_duration',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0',
})
# ---

View File

@ -1,19 +1,20 @@
"""Tests for the Tedee Binary Sensors.""" """Tests for the Tedee Binary Sensors."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from aiotedee import TedeeLock from aiotedee import TedeeLock
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import async_fire_time_changed from . import setup_integration
pytestmark = pytest.mark.usefixtures("init_integration") from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
BINARY_SENSORS = ("charging", "semi_locked", "pullspring_enabled", "lock_uncalibrated") BINARY_SENSORS = ("charging", "semi_locked", "pullspring_enabled", "lock_uncalibrated")
@ -22,21 +23,19 @@ BINARY_SENSORS = ("charging", "semi_locked", "pullspring_enabled", "lock_uncalib
async def test_binary_sensors( async def test_binary_sensors(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test tedee binary sensor.""" """Test tedee binary sensor."""
for key in BINARY_SENSORS: with patch("homeassistant.components.tedee.PLATFORMS", [Platform.BINARY_SENSOR]):
state = hass.states.get(f"binary_sensor.lock_1a2b_{key}") await setup_integration(hass, mock_config_entry)
assert state
assert state == snapshot(name=f"state-{key}")
entry = entity_registry.async_get(state.entity_id) await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
assert entry
assert entry == snapshot(name=f"entry-{key}")
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.usefixtures("init_integration")
async def test_new_binary_sensors( async def test_new_binary_sensors(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,

View File

@ -20,6 +20,7 @@ from homeassistant.const import CONF_HOST, CONF_WEBHOOK_ID, EVENT_HOMEASSISTANT_
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from . import setup_integration
from .conftest import WEBHOOK_ID from .conftest import WEBHOOK_ID
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -32,9 +33,7 @@ async def test_load_unload_config_entry(
mock_tedee: MagicMock, mock_tedee: MagicMock,
) -> None: ) -> None:
"""Test loading and unloading the integration.""" """Test loading and unloading the integration."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
@ -56,9 +55,7 @@ async def test_config_entry_not_ready(
"""Test the Tedee configuration entry not ready.""" """Test the Tedee configuration entry not ready."""
mock_tedee.get_locks.side_effect = side_effect mock_tedee.get_locks.side_effect = side_effect
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert len(mock_tedee.get_locks.mock_calls) == 1 assert len(mock_tedee.get_locks.mock_calls) == 1
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
@ -70,9 +67,7 @@ async def test_cleanup_on_shutdown(
mock_tedee: MagicMock, mock_tedee: MagicMock,
) -> None: ) -> None:
"""Test the webhook is cleaned up on shutdown.""" """Test the webhook is cleaned up on shutdown."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
@ -88,9 +83,7 @@ async def test_webhook_cleanup_errors(
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test the webhook is cleaned up on shutdown.""" """Test the webhook is cleaned up on shutdown."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
@ -110,9 +103,7 @@ async def test_webhook_registration_errors(
) -> None: ) -> None:
"""Test the webhook is cleaned up on shutdown.""" """Test the webhook is cleaned up on shutdown."""
mock_tedee.register_webhook.side_effect = TedeeWebhookException("") mock_tedee.register_webhook.side_effect = TedeeWebhookException("")
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
@ -128,9 +119,7 @@ async def test_webhook_registration_cleanup_errors(
) -> None: ) -> None:
"""Test the errors during webhook cleanup during registration.""" """Test the errors during webhook cleanup during registration."""
mock_tedee.cleanup_webhooks_by_host.side_effect = TedeeWebhookException("") mock_tedee.cleanup_webhooks_by_host.side_effect = TedeeWebhookException("")
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED assert mock_config_entry.state is ConfigEntryState.LOADED
@ -138,6 +127,21 @@ async def test_webhook_registration_cleanup_errors(
assert "Failed to cleanup Tedee webhooks by host:" in caplog.text assert "Failed to cleanup Tedee webhooks by host:" in caplog.text
async def test_lock_device(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_tedee: MagicMock,
device_registry: dr.DeviceRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Ensure the lock device is registered."""
await setup_integration(hass, mock_config_entry)
device = device_registry.async_get_device({(mock_config_entry.domain, "12345")})
assert device
assert device == snapshot
async def test_bridge_device( async def test_bridge_device(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
@ -146,9 +150,7 @@ async def test_bridge_device(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Ensure the bridge device is registered.""" """Ensure the bridge device is registered."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
device = device_registry.async_get_device( device = device_registry.async_get_device(
{(mock_config_entry.domain, mock_tedee.get_local_bridge.return_value.serial)} {(mock_config_entry.domain, mock_tedee.get_local_bridge.return_value.serial)}
@ -192,9 +194,7 @@ async def test_webhook_post(
) -> None: ) -> None:
"""Test webhook callback.""" """Test webhook callback."""
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
client = await hass_client_no_auth() client = await hass_client_no_auth()
webhook_url = async_generate_url(hass, WEBHOOK_ID) webhook_url = async_generate_url(hass, WEBHOOK_ID)
@ -241,9 +241,7 @@ async def test_migration(
"homeassistant.components.tedee.webhook_generate_id", "homeassistant.components.tedee.webhook_generate_id",
return_value=WEBHOOK_ID, return_value=WEBHOOK_ID,
): ):
mock_config_entry.add_to_hass(hass) await setup_integration(hass, mock_config_entry)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.version == 1 assert mock_config_entry.version == 1
assert mock_config_entry.minor_version == 2 assert mock_config_entry.minor_version == 2

View File

@ -1,7 +1,7 @@
"""Tests for tedee lock.""" """Tests for tedee lock."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from urllib.parse import urlparse from urllib.parse import urlparse
from aiotedee import TedeeLock, TedeeLockState from aiotedee import TedeeLock, TedeeLockState
@ -22,43 +22,44 @@ from homeassistant.components.lock import (
LockState, LockState,
) )
from homeassistant.components.webhook import async_generate_url from homeassistant.components.webhook import async_generate_url
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.const import (
ATTR_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceNotSupported from homeassistant.exceptions import HomeAssistantError, ServiceNotSupported
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import setup_integration
from .conftest import WEBHOOK_ID from .conftest import WEBHOOK_ID
from tests.common import MockConfigEntry, async_fire_time_changed from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
pytestmark = pytest.mark.usefixtures("init_integration")
async def test_locks(
async def test_lock(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
device_registry: dr.DeviceRegistry, mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None:
"""Test tedee locks."""
with patch("homeassistant.components.tedee.PLATFORMS", [Platform.LOCK]):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.usefixtures("init_integration")
async def test_lock_service_calls(
hass: HomeAssistant,
mock_tedee: MagicMock,
) -> None: ) -> None:
"""Test the tedee lock.""" """Test the tedee lock."""
mock_tedee.lock.return_value = None
mock_tedee.unlock.return_value = None
mock_tedee.open.return_value = None
state = hass.states.get("lock.lock_1a2b")
assert state
assert state == snapshot
entry = entity_registry.async_get(state.entity_id)
assert entry
assert entry == snapshot
assert entry.device_id
device = device_registry.async_get(entry.device_id)
assert device == snapshot
await hass.services.async_call( await hass.services.async_call(
LOCK_DOMAIN, LOCK_DOMAIN,
@ -106,6 +107,7 @@ async def test_lock(
assert state.state == LockState.UNLOCKING assert state.state == LockState.UNLOCKING
@pytest.mark.usefixtures("init_integration")
async def test_lock_without_pullspring( async def test_lock_without_pullspring(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
@ -116,9 +118,6 @@ async def test_lock_without_pullspring(
"""Test the tedee lock without pullspring.""" """Test the tedee lock without pullspring."""
# Fetch translations # Fetch translations
await async_setup_component(hass, "homeassistant", {}) await async_setup_component(hass, "homeassistant", {})
mock_tedee.lock.return_value = None
mock_tedee.unlock.return_value = None
mock_tedee.open.return_value = None
state = hass.states.get("lock.lock_2c3d") state = hass.states.get("lock.lock_2c3d")
assert state assert state
@ -149,6 +148,7 @@ async def test_lock_without_pullspring(
assert len(mock_tedee.open.mock_calls) == 0 assert len(mock_tedee.open.mock_calls) == 0
@pytest.mark.usefixtures("init_integration")
async def test_lock_errors( async def test_lock_errors(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
@ -191,6 +191,7 @@ async def test_lock_errors(
assert exc_info.value.translation_key == "open_failed" assert exc_info.value.translation_key == "open_failed"
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize( @pytest.mark.parametrize(
"side_effect", "side_effect",
[ [
@ -217,6 +218,7 @@ async def test_update_failed(
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@pytest.mark.usefixtures("init_integration")
async def test_cleanup_removed_locks( async def test_cleanup_removed_locks(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
@ -247,6 +249,7 @@ async def test_cleanup_removed_locks(
assert "Lock-1A2B" not in locks assert "Lock-1A2B" not in locks
@pytest.mark.usefixtures("init_integration")
async def test_new_lock( async def test_new_lock(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
@ -275,6 +278,7 @@ async def test_new_lock(
assert state assert state
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize( @pytest.mark.parametrize(
("lib_state", "expected_state"), ("lib_state", "expected_state"),
[ [

View File

@ -1,20 +1,20 @@
"""Tests for the Tedee Sensors.""" """Tests for the Tedee Sensors."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from aiotedee import TedeeLock from aiotedee import TedeeLock
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import async_fire_time_changed from . import setup_integration
pytestmark = pytest.mark.usefixtures("init_integration")
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
SENSORS = ( SENSORS = (
"battery", "battery",
@ -25,21 +25,18 @@ SENSORS = (
async def test_sensors( async def test_sensors(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test tedee sensors.""" """Test tedee sensors."""
for key in SENSORS: with patch("homeassistant.components.tedee.PLATFORMS", [Platform.SENSOR]):
state = hass.states.get(f"sensor.lock_1a2b_{key}") await setup_integration(hass, mock_config_entry)
assert state
assert state == snapshot(name=f"state-{key}")
entry = entity_registry.async_get(state.entity_id) await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
assert entry
assert entry.device_id
assert entry == snapshot(name=f"entry-{key}")
@pytest.mark.usefixtures("init_integration")
async def test_new_sensors( async def test_new_sensors(
hass: HomeAssistant, hass: HomeAssistant,
mock_tedee: MagicMock, mock_tedee: MagicMock,