diff --git a/homeassistant/components/nina/quality_scale.yaml b/homeassistant/components/nina/quality_scale.yaml index 8baa258e1d1..797e101c15a 100644 --- a/homeassistant/components/nina/quality_scale.yaml +++ b/homeassistant/components/nina/quality_scale.yaml @@ -50,7 +50,6 @@ rules: Use load_json_object_fixture in tests Patch the library instead of the HTTP requests Create a shared fixture for the mock config entry - Use snapshots for binary sensor tests Use init_integration in tests Evaluate the need of test_config_entry_not_ready diff --git a/tests/components/nina/__init__.py b/tests/components/nina/__init__.py index 702bd78715b..29e3bdfea08 100644 --- a/tests/components/nina/__init__.py +++ b/tests/components/nina/__init__.py @@ -2,8 +2,24 @@ import json from typing import Any +from unittest.mock import patch -from tests.common import load_fixture +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry, load_fixture + + +async def setup_platform(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Set up the NINA platform.""" + with patch( + "pynina.baseApi.BaseAPI._makeRequest", + wraps=mocked_request_function, + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED def mocked_request_function(url: str) -> dict[str, Any]: diff --git a/tests/components/nina/snapshots/test_binary_sensor.ambr b/tests/components/nina/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..0da4435a5d0 --- /dev/null +++ b/tests/components/nina/snapshots/test_binary_sensor.ambr @@ -0,0 +1,257 @@ +# serializer version: 1 +# name: test_sensors[binary_sensor.nina_warning_aach_1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.nina_warning_aach_1', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Warning: Aach 1', + 'platform': 'nina', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '095760000000-1', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_1-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'affected_areas': 'Gemeinde Oberreichenbach, Gemeinde Neuweiler, Stadt Nagold, Stadt Neubulach, Gemeinde Schömberg, Gemeinde Simmersfeld, Gemeinde Simmozheim, Gemeinde Rohrdorf, Gemeinde Ostelsheim, Gemeinde Ebhausen, Gemeinde Egenhausen, Gemeinde Dobel, Stadt Bad Liebenzell, Stadt Solingen, Stadt Haiterbach, Stadt Bad Herrenalb, Gemeinde Höfen an der Enz, Gemeinde Gechingen, Gemeinde Enzklösterle, Gemeinde Gutach (Schwarzwaldbahn) und 3392 weitere.', + 'description': 'Es treten Sturmböen mit Geschwindigkeiten zwischen 70 km/h (20m/s, 38kn, Bft 8) und 85 km/h (24m/s, 47kn, Bft 9) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen bis 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden.', + 'device_class': 'safety', + 'expires': '3021-11-22T05:19:00+01:00', + 'friendly_name': 'NINA Warning: Aach 1', + 'headline': 'Ausfall Notruf 112', + 'id': 'mow.DE-NW-BN-SE030-20201014-30-000', + 'recommended_actions': '', + 'sender': 'Deutscher Wetterdienst', + 'sent': '2021-10-11T05:20:00+01:00', + 'severity': 'Minor', + 'start': '2021-11-01T05:20:00+01:00', + 'web': 'https://www.wettergefahren.de', + }), + 'context': , + 'entity_id': 'binary_sensor.nina_warning_aach_1', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.nina_warning_aach_2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Warning: Aach 2', + 'platform': 'nina', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '095760000000-2', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'safety', + 'friendly_name': 'NINA Warning: Aach 2', + }), + 'context': , + 'entity_id': 'binary_sensor.nina_warning_aach_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_3-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.nina_warning_aach_3', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Warning: Aach 3', + 'platform': 'nina', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '095760000000-3', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_3-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'safety', + 'friendly_name': 'NINA Warning: Aach 3', + }), + 'context': , + 'entity_id': 'binary_sensor.nina_warning_aach_3', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_4-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.nina_warning_aach_4', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Warning: Aach 4', + 'platform': 'nina', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '095760000000-4', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_4-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'safety', + 'friendly_name': 'NINA Warning: Aach 4', + }), + 'context': , + 'entity_id': 'binary_sensor.nina_warning_aach_4', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_5-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.nina_warning_aach_5', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Warning: Aach 5', + 'platform': 'nina', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '095760000000-5', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.nina_warning_aach_5-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'safety', + 'friendly_name': 'NINA Warning: Aach 5', + }), + 'context': , + 'entity_id': 'binary_sensor.nina_warning_aach_5', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/nina/test_binary_sensor.py b/tests/components/nina/test_binary_sensor.py index daaabade423..9aae30be29c 100644 --- a/tests/components/nina/test_binary_sensor.py +++ b/tests/components/nina/test_binary_sensor.py @@ -3,40 +3,17 @@ from __future__ import annotations from typing import Any -from unittest.mock import patch -from homeassistant.components.binary_sensor import BinarySensorDeviceClass -from homeassistant.components.nina.const import ( - ATTR_AFFECTED_AREAS, - ATTR_DESCRIPTION, - ATTR_EXPIRES, - ATTR_HEADLINE, - ATTR_ID, - ATTR_RECOMMENDED_ACTIONS, - ATTR_SENDER, - ATTR_SENT, - ATTR_SEVERITY, - ATTR_START, - ATTR_WEB, - DOMAIN, -) -from homeassistant.config_entries import ConfigEntryState +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.nina.const import ATTR_HEADLINE, DOMAIN from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from . import mocked_request_function +from . import setup_platform -from tests.common import MockConfigEntry - -ENTRY_DATA: dict[str, Any] = { - "slots": 5, - "regions": {"083350000000": "Aach, Stadt"}, - "filters": { - "headline_filter": ".*corona.*", - "area_filter": ".*", - }, -} +from tests.common import MockConfigEntry, snapshot_platform ENTRY_DATA_NO_CORONA: dict[str, Any] = { "slots": 5, @@ -47,7 +24,7 @@ ENTRY_DATA_NO_CORONA: dict[str, Any] = { }, } -ENTRY_DATA_NO_AREA: dict[str, Any] = { +ENTRY_DATA_SPECIFIC_AREA: dict[str, Any] = { "slots": 5, "regions": {"083350000000": "Aach, Stadt"}, "filters": { @@ -57,321 +34,93 @@ ENTRY_DATA_NO_AREA: dict[str, Any] = { } -async def test_sensors(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None: +async def test_sensors( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: """Test the creation and values of the NINA sensors.""" - with patch( - "pynina.baseApi.BaseAPI._makeRequest", - wraps=mocked_request_function, - ): - conf_entry: MockConfigEntry = MockConfigEntry( - domain=DOMAIN, title="NINA", data=ENTRY_DATA, version=1, minor_version=3 - ) - conf_entry.add_to_hass(hass) - - await hass.config_entries.async_setup(conf_entry.entry_id) - await hass.async_block_till_done() - - assert conf_entry.state is ConfigEntryState.LOADED - - state_w1 = hass.states.get("binary_sensor.nina_warning_aach_stadt_1") - entry_w1 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_1") - - assert state_w1.state == STATE_ON - assert state_w1.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112" - assert ( - state_w1.attributes.get(ATTR_DESCRIPTION) - == "Es treten Sturmböen mit Geschwindigkeiten zwischen 70 km/h (20m/s, 38kn, Bft 8) und 85 km/h (24m/s, 47kn, Bft 9) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen bis 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden." - ) - assert state_w1.attributes.get(ATTR_SENDER) == "Deutscher Wetterdienst" - assert state_w1.attributes.get(ATTR_SEVERITY) == "Minor" - assert state_w1.attributes.get(ATTR_RECOMMENDED_ACTIONS) == "" - assert state_w1.attributes.get(ATTR_WEB) == "https://www.wettergefahren.de" - assert ( - state_w1.attributes.get(ATTR_AFFECTED_AREAS) - == "Gemeinde Oberreichenbach, Gemeinde Neuweiler, Stadt Nagold, Stadt Neubulach, Gemeinde Schömberg, Gemeinde Simmersfeld, Gemeinde Simmozheim, Gemeinde Rohrdorf, Gemeinde Ostelsheim, Gemeinde Ebhausen, Gemeinde Egenhausen, Gemeinde Dobel, Stadt Bad Liebenzell, Stadt Solingen, Stadt Haiterbach, Stadt Bad Herrenalb, Gemeinde Höfen an der Enz, Gemeinde Gechingen, Gemeinde Enzklösterle, Gemeinde Gutach (Schwarzwaldbahn) und 3392 weitere." - ) - assert state_w1.attributes.get(ATTR_ID) == "mow.DE-NW-BN-SE030-20201014-30-000" - assert state_w1.attributes.get(ATTR_SENT) == "2021-10-11T05:20:00+01:00" - assert state_w1.attributes.get(ATTR_START) == "2021-11-01T05:20:00+01:00" - assert state_w1.attributes.get(ATTR_EXPIRES) == "3021-11-22T05:19:00+01:00" - - assert entry_w1.unique_id == "083350000000-1" - assert state_w1.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w2 = hass.states.get("binary_sensor.nina_warning_aach_stadt_2") - entry_w2 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_2") - - assert state_w2.state == STATE_OFF - assert state_w2.attributes.get(ATTR_HEADLINE) is None - assert state_w2.attributes.get(ATTR_DESCRIPTION) is None - assert state_w2.attributes.get(ATTR_SENDER) is None - assert state_w2.attributes.get(ATTR_SEVERITY) is None - assert state_w2.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w2.attributes.get(ATTR_WEB) is None - assert state_w2.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w2.attributes.get(ATTR_ID) is None - assert state_w2.attributes.get(ATTR_SENT) is None - assert state_w2.attributes.get(ATTR_START) is None - assert state_w2.attributes.get(ATTR_EXPIRES) is None - - assert entry_w2.unique_id == "083350000000-2" - assert state_w2.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w3 = hass.states.get("binary_sensor.nina_warning_aach_stadt_3") - entry_w3 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_3") - - assert state_w3.state == STATE_OFF - assert state_w3.attributes.get(ATTR_HEADLINE) is None - assert state_w3.attributes.get(ATTR_DESCRIPTION) is None - assert state_w3.attributes.get(ATTR_SENDER) is None - assert state_w3.attributes.get(ATTR_SEVERITY) is None - assert state_w3.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w3.attributes.get(ATTR_WEB) is None - assert state_w3.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w3.attributes.get(ATTR_ID) is None - assert state_w3.attributes.get(ATTR_SENT) is None - assert state_w3.attributes.get(ATTR_START) is None - assert state_w3.attributes.get(ATTR_EXPIRES) is None - - assert entry_w3.unique_id == "083350000000-3" - assert state_w3.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w4 = hass.states.get("binary_sensor.nina_warning_aach_stadt_4") - entry_w4 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_4") - - assert state_w4.state == STATE_OFF - assert state_w4.attributes.get(ATTR_HEADLINE) is None - assert state_w4.attributes.get(ATTR_DESCRIPTION) is None - assert state_w4.attributes.get(ATTR_SENDER) is None - assert state_w4.attributes.get(ATTR_SEVERITY) is None - assert state_w4.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w4.attributes.get(ATTR_WEB) is None - assert state_w4.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w4.attributes.get(ATTR_ID) is None - assert state_w4.attributes.get(ATTR_SENT) is None - assert state_w4.attributes.get(ATTR_START) is None - assert state_w4.attributes.get(ATTR_EXPIRES) is None - - assert entry_w4.unique_id == "083350000000-4" - assert state_w4.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w5 = hass.states.get("binary_sensor.nina_warning_aach_stadt_5") - entry_w5 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_5") - - assert state_w5.state == STATE_OFF - assert state_w5.attributes.get(ATTR_HEADLINE) is None - assert state_w5.attributes.get(ATTR_DESCRIPTION) is None - assert state_w5.attributes.get(ATTR_SENDER) is None - assert state_w5.attributes.get(ATTR_SEVERITY) is None - assert state_w5.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w5.attributes.get(ATTR_WEB) is None - assert state_w5.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w5.attributes.get(ATTR_ID) is None - assert state_w5.attributes.get(ATTR_SENT) is None - assert state_w5.attributes.get(ATTR_START) is None - assert state_w5.attributes.get(ATTR_EXPIRES) is None - - assert entry_w5.unique_id == "083350000000-5" - assert state_w5.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + await setup_platform(hass, mock_config_entry) + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) async def test_sensors_without_corona_filter( - hass: HomeAssistant, entity_registry: er.EntityRegistry + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion ) -> None: """Test the creation and values of the NINA sensors without the corona filter.""" - with patch( - "pynina.baseApi.BaseAPI._makeRequest", - wraps=mocked_request_function, - ): - conf_entry: MockConfigEntry = MockConfigEntry( - domain=DOMAIN, - title="NINA", - data=ENTRY_DATA_NO_CORONA, - version=1, - minor_version=3, - ) - conf_entry.add_to_hass(hass) + conf_entry: MockConfigEntry = MockConfigEntry( + domain=DOMAIN, + title="NINA", + data=ENTRY_DATA_NO_CORONA, + version=1, + minor_version=3, + ) + conf_entry.add_to_hass(hass) - await hass.config_entries.async_setup(conf_entry.entry_id) - await hass.async_block_till_done() + await setup_platform(hass, conf_entry) - assert conf_entry.state is ConfigEntryState.LOADED + state_w1 = hass.states.get("binary_sensor.nina_warning_aach_stadt_1") - state_w1 = hass.states.get("binary_sensor.nina_warning_aach_stadt_1") - entry_w1 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_1") + assert state_w1.state == STATE_ON + assert ( + state_w1.attributes.get(ATTR_HEADLINE) + == "Corona-Verordnung des Landes: Warnstufe durch Landesgesundheitsamt ausgerufen" + ) - assert state_w1.state == STATE_ON - assert ( - state_w1.attributes.get(ATTR_HEADLINE) - == "Corona-Verordnung des Landes: Warnstufe durch Landesgesundheitsamt ausgerufen" - ) - assert ( - state_w1.attributes.get(ATTR_DESCRIPTION) - == "Die Zahl der mit dem Corona-Virus infizierten Menschen steigt gegenwärtig stark an. Es wächst daher die Gefahr einer weiteren Verbreitung der Infektion und - je nach Einzelfall - auch von schweren Erkrankungen." - ) - assert state_w1.attributes.get(ATTR_SENDER) == "" - assert state_w1.attributes.get(ATTR_SEVERITY) == "Minor" - assert ( - state_w1.attributes.get(ATTR_RECOMMENDED_ACTIONS) - == "Waschen sich regelmäßig und gründlich die Hände." - ) - assert state_w1.attributes.get(ATTR_WEB) == "" - assert ( - state_w1.attributes.get(ATTR_AFFECTED_AREAS) - == "Bundesland: Freie Hansestadt Bremen, Land Berlin, Land Hessen, Land Nordrhein-Westfalen, Land Brandenburg, Freistaat Bayern, Land Mecklenburg-Vorpommern, Land Rheinland-Pfalz, Freistaat Sachsen, Land Schleswig-Holstein, Freie und Hansestadt Hamburg, Freistaat Thüringen, Land Niedersachsen, Land Saarland, Land Sachsen-Anhalt, Land Baden-Württemberg" - ) - assert state_w1.attributes.get(ATTR_ID) == "mow.DE-BW-S-SE018-20211102-18-001" - assert state_w1.attributes.get(ATTR_SENT) == "2021-11-02T20:07:16+01:00" - assert state_w1.attributes.get(ATTR_START) == "" - assert state_w1.attributes.get(ATTR_EXPIRES) == "" + state_w2 = hass.states.get("binary_sensor.nina_warning_aach_stadt_2") - assert entry_w1.unique_id == "083350000000-1" - assert state_w1.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + assert state_w2.state == STATE_ON + assert state_w2.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112" - state_w2 = hass.states.get("binary_sensor.nina_warning_aach_stadt_2") - entry_w2 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_2") + state_w3 = hass.states.get("binary_sensor.nina_warning_aach_stadt_3") - assert state_w2.state == STATE_ON - assert state_w2.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112" - assert ( - state_w2.attributes.get(ATTR_DESCRIPTION) - == "Es treten Sturmböen mit Geschwindigkeiten zwischen 70 km/h (20m/s, 38kn, Bft 8) und 85 km/h (24m/s, 47kn, Bft 9) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen bis 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden." - ) - assert ( - state_w2.attributes.get(ATTR_AFFECTED_AREAS) - == "Gemeinde Oberreichenbach, Gemeinde Neuweiler, Stadt Nagold, Stadt Neubulach, Gemeinde Schömberg, Gemeinde Simmersfeld, Gemeinde Simmozheim, Gemeinde Rohrdorf, Gemeinde Ostelsheim, Gemeinde Ebhausen, Gemeinde Egenhausen, Gemeinde Dobel, Stadt Bad Liebenzell, Stadt Solingen, Stadt Haiterbach, Stadt Bad Herrenalb, Gemeinde Höfen an der Enz, Gemeinde Gechingen, Gemeinde Enzklösterle, Gemeinde Gutach (Schwarzwaldbahn) und 3392 weitere." - ) - assert state_w2.attributes.get(ATTR_SENDER) == "Deutscher Wetterdienst" - assert state_w2.attributes.get(ATTR_SEVERITY) == "Minor" - assert state_w2.attributes.get(ATTR_RECOMMENDED_ACTIONS) == "" - assert state_w2.attributes.get(ATTR_WEB) == "https://www.wettergefahren.de" - assert state_w2.attributes.get(ATTR_ID) == "mow.DE-NW-BN-SE030-20201014-30-000" - assert state_w2.attributes.get(ATTR_SENT) == "2021-10-11T05:20:00+01:00" - assert state_w2.attributes.get(ATTR_START) == "2021-11-01T05:20:00+01:00" - assert state_w2.attributes.get(ATTR_EXPIRES) == "3021-11-22T05:19:00+01:00" + assert state_w3.state == STATE_OFF - assert entry_w2.unique_id == "083350000000-2" - assert state_w2.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + state_w4 = hass.states.get("binary_sensor.nina_warning_aach_stadt_4") - state_w3 = hass.states.get("binary_sensor.nina_warning_aach_stadt_3") - entry_w3 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_3") + assert state_w4.state == STATE_OFF - assert state_w3.state == STATE_OFF - assert state_w3.attributes.get(ATTR_HEADLINE) is None - assert state_w3.attributes.get(ATTR_DESCRIPTION) is None - assert state_w3.attributes.get(ATTR_SENDER) is None - assert state_w3.attributes.get(ATTR_SEVERITY) is None - assert state_w3.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w3.attributes.get(ATTR_WEB) is None - assert state_w3.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w3.attributes.get(ATTR_ID) is None - assert state_w3.attributes.get(ATTR_SENT) is None - assert state_w3.attributes.get(ATTR_START) is None - assert state_w3.attributes.get(ATTR_EXPIRES) is None + state_w5 = hass.states.get("binary_sensor.nina_warning_aach_stadt_5") - assert entry_w3.unique_id == "083350000000-3" - assert state_w3.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w4 = hass.states.get("binary_sensor.nina_warning_aach_stadt_4") - entry_w4 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_4") - - assert state_w4.state == STATE_OFF - assert state_w4.attributes.get(ATTR_HEADLINE) is None - assert state_w4.attributes.get(ATTR_DESCRIPTION) is None - assert state_w4.attributes.get(ATTR_SENDER) is None - assert state_w4.attributes.get(ATTR_SEVERITY) is None - assert state_w4.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w4.attributes.get(ATTR_WEB) is None - assert state_w4.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w4.attributes.get(ATTR_ID) is None - assert state_w4.attributes.get(ATTR_SENT) is None - assert state_w4.attributes.get(ATTR_START) is None - assert state_w4.attributes.get(ATTR_EXPIRES) is None - - assert entry_w4.unique_id == "083350000000-4" - assert state_w4.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w5 = hass.states.get("binary_sensor.nina_warning_aach_stadt_5") - entry_w5 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_5") - - assert state_w5.state == STATE_OFF - assert state_w5.attributes.get(ATTR_HEADLINE) is None - assert state_w5.attributes.get(ATTR_DESCRIPTION) is None - assert state_w5.attributes.get(ATTR_SENDER) is None - assert state_w5.attributes.get(ATTR_SEVERITY) is None - assert state_w5.attributes.get(ATTR_RECOMMENDED_ACTIONS) is None - assert state_w5.attributes.get(ATTR_WEB) is None - assert state_w5.attributes.get(ATTR_AFFECTED_AREAS) is None - assert state_w5.attributes.get(ATTR_ID) is None - assert state_w5.attributes.get(ATTR_SENT) is None - assert state_w5.attributes.get(ATTR_START) is None - assert state_w5.attributes.get(ATTR_EXPIRES) is None - - assert entry_w5.unique_id == "083350000000-5" - assert state_w5.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + assert state_w5.state == STATE_OFF async def test_sensors_with_area_filter( - hass: HomeAssistant, entity_registry: er.EntityRegistry + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion ) -> None: - """Test the creation and values of the NINA sensors with an area filter.""" + """Test the creation and values of the NINA sensors with a restrictive area filter.""" - with patch( - "pynina.baseApi.BaseAPI._makeRequest", - wraps=mocked_request_function, - ): - conf_entry: MockConfigEntry = MockConfigEntry( - domain=DOMAIN, - title="NINA", - data=ENTRY_DATA_NO_AREA, - version=1, - minor_version=3, - ) - conf_entry.add_to_hass(hass) + conf_entry: MockConfigEntry = MockConfigEntry( + domain=DOMAIN, + title="NINA", + data=ENTRY_DATA_SPECIFIC_AREA, + version=1, + minor_version=3, + ) + conf_entry.add_to_hass(hass) - await hass.config_entries.async_setup(conf_entry.entry_id) - await hass.async_block_till_done() + await setup_platform(hass, conf_entry) - assert conf_entry.state is ConfigEntryState.LOADED + state_w1 = hass.states.get("binary_sensor.nina_warning_aach_stadt_1") - state_w1 = hass.states.get("binary_sensor.nina_warning_aach_stadt_1") - entry_w1 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_1") + assert state_w1.state == STATE_ON + assert state_w1.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112" - assert state_w1.state == STATE_ON + state_w2 = hass.states.get("binary_sensor.nina_warning_aach_stadt_2") - assert entry_w1.unique_id == "083350000000-1" - assert state_w1.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + assert state_w2.state == STATE_OFF - state_w2 = hass.states.get("binary_sensor.nina_warning_aach_stadt_2") - entry_w2 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_2") + state_w3 = hass.states.get("binary_sensor.nina_warning_aach_stadt_3") - assert state_w2.state == STATE_OFF + assert state_w3.state == STATE_OFF - assert entry_w2.unique_id == "083350000000-2" - assert state_w2.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + state_w4 = hass.states.get("binary_sensor.nina_warning_aach_stadt_4") - state_w3 = hass.states.get("binary_sensor.nina_warning_aach_stadt_3") - entry_w3 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_3") + assert state_w4.state == STATE_OFF - assert state_w3.state == STATE_OFF + state_w5 = hass.states.get("binary_sensor.nina_warning_aach_stadt_5") - assert entry_w3.unique_id == "083350000000-3" - assert state_w3.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w4 = hass.states.get("binary_sensor.nina_warning_aach_stadt_4") - entry_w4 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_4") - - assert state_w4.state == STATE_OFF - - assert entry_w4.unique_id == "083350000000-4" - assert state_w4.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY - - state_w5 = hass.states.get("binary_sensor.nina_warning_aach_stadt_5") - entry_w5 = entity_registry.async_get("binary_sensor.nina_warning_aach_stadt_5") - - assert state_w5.state == STATE_OFF - - assert entry_w5.unique_id == "083350000000-5" - assert state_w5.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY + assert state_w5.state == STATE_OFF diff --git a/tests/components/nina/test_config_flow.py b/tests/components/nina/test_config_flow.py index b9b1e11f0db..4bde1891a80 100644 --- a/tests/components/nina/test_config_flow.py +++ b/tests/components/nina/test_config_flow.py @@ -3,7 +3,6 @@ from __future__ import annotations from copy import deepcopy -import json from typing import Any from unittest.mock import AsyncMock, patch @@ -28,14 +27,10 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import entity_registry as er -from . import mocked_request_function +from . import mocked_request_function, setup_platform from .const import DUMMY_CONFIG_ENTRY, DUMMY_USER_INPUT -from tests.common import MockConfigEntry, load_fixture - -DUMMY_RESPONSE_REGIONS: dict[str, Any] = json.loads( - load_fixture("sample_regions.json", "nina") -) +from tests.common import MockConfigEntry def assert_dummy_entry_created(result: dict[str, Any]) -> None: @@ -141,15 +136,15 @@ async def test_options_flow_init( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: """Test config flow options.""" + + await setup_platform(hass, mock_config_entry) + with ( patch( "pynina.baseApi.BaseAPI._makeRequest", wraps=mocked_request_function, ), ): - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init( mock_config_entry.entry_id ) @@ -195,15 +190,15 @@ async def test_options_flow_with_no_selection( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: """Test config flow options with no selection.""" + + await setup_platform(hass, mock_config_entry) + with ( patch( "pynina.baseApi.BaseAPI._makeRequest", wraps=mocked_request_function, ), ): - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init( mock_config_entry.entry_id ) @@ -264,13 +259,13 @@ async def test_options_flow_connection_error( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: """Test config flow options but no connection.""" + + await setup_platform(hass, mock_config_entry) + with patch( "pynina.baseApi.BaseAPI._makeRequest", side_effect=ApiError("Could not connect to Api"), ): - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init( mock_config_entry.entry_id ) @@ -283,15 +278,15 @@ async def test_options_flow_unexpected_exception( hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_config_entry: MockConfigEntry ) -> None: """Test config flow options but with an unexpected exception.""" + + await setup_platform(hass, mock_config_entry) + with ( patch( "pynina.baseApi.BaseAPI._makeRequest", side_effect=Exception("DUMMY"), ), ): - await hass.config_entries.async_setup(mock_config_entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init( mock_config_entry.entry_id ) @@ -313,15 +308,14 @@ async def test_options_flow_entity_removal( ) config_entry.add_to_hass(hass) + await setup_platform(hass, config_entry) + with ( patch( "pynina.baseApi.BaseAPI._makeRequest", wraps=mocked_request_function, ), ): - await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init(config_entry.entry_id) result = await hass.config_entries.options.async_configure(