From 7aaeb3763b24107c103d045062429764bd4ffb51 Mon Sep 17 00:00:00 2001 From: Emanuele Palazzetti Date: Wed, 18 Oct 2023 22:17:58 +0200 Subject: [PATCH] feat(experimental): add `force_update` as experimental setting in YAML conf (#85) https://github.com/palazzem/ha-econnect-alarm/pull/85 --- .gitignore | 3 +++ custom_components/econnect_metronet/__init__.py | 13 +++++++++---- .../econnect_metronet/binary_sensor.py | 9 +++++++++ tests/conftest.py | 8 ++++++++ tests/test_experiments.py | 15 +++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/test_experiments.py diff --git a/.gitignore b/.gitignore index 68bc17f..09e0029 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# VSCode +.vscode/ diff --git a/custom_components/econnect_metronet/__init__.py b/custom_components/econnect_metronet/__init__.py index f9b1e2a..6821404 100644 --- a/custom_components/econnect_metronet/__init__.py +++ b/custom_components/econnect_metronet/__init__.py @@ -4,7 +4,7 @@ from elmo.api.client import ElmoClient from elmo.systems import ELMO_E_CONNECT as E_CONNECT_DEFAULT -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntry, ConfigType from homeassistant.core import HomeAssistant from .const import ( @@ -41,9 +41,14 @@ async def async_migrate_entry(hass, config: ConfigEntry): return True -async def async_setup(hass: HomeAssistant, config: dict): - """Set up the E-connect Alarm component.""" - hass.data[DOMAIN] = {} +async def async_setup(hass: HomeAssistant, config: ConfigType): + """Initialize the E-connect Alarm integration. + + This method exposes eventual YAML configuration options under the DOMAIN key. + Use YAML configurations only to expose experimental settings, otherwise use + the configuration flow. + """ + hass.data[DOMAIN] = config.get(DOMAIN, {}) return True diff --git a/custom_components/econnect_metronet/binary_sensor.py b/custom_components/econnect_metronet/binary_sensor.py index 5d9db8f..904eeb3 100644 --- a/custom_components/econnect_metronet/binary_sensor.py +++ b/custom_components/econnect_metronet/binary_sensor.py @@ -61,6 +61,9 @@ def __init__( device: AlarmDevice, ) -> None: """Construct.""" + # Enable experimental settings from the configuration file + self._attr_force_update = coordinator.hass.data[DOMAIN].get("force_update", False) + super().__init__(coordinator) self.entity_id = generate_entity_id(config, name) self._name = name @@ -113,6 +116,9 @@ def __init__( device: AlarmDevice, ) -> None: """Construct.""" + # Enable experimental settings from the configuration file + self._attr_force_update = coordinator.hass.data[DOMAIN].get("force_update", False) + super().__init__(coordinator) self.entity_id = generate_entity_id(config, name) self._name = name @@ -156,6 +162,9 @@ def __init__( device: AlarmDevice, ) -> None: """Construct.""" + # Enable experimental settings from the configuration file + self._attr_force_update = coordinator.hass.data[DOMAIN].get("force_update", False) + super().__init__(coordinator) self.entity_id = generate_entity_id(config, name) self._name = name diff --git a/tests/conftest.py b/tests/conftest.py index 6de2882..6ea00b1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ from elmo.api.client import ElmoClient from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from custom_components.econnect_metronet import async_setup from custom_components.econnect_metronet.alarm_control_panel import EconnectAlarm from custom_components.econnect_metronet.coordinator import AlarmCoordinator from custom_components.econnect_metronet.devices import AlarmDevice @@ -16,6 +17,13 @@ @pytest.fixture async def hass(hass): + """Create a Home Assistant instance for testing. + + This fixture forces some settings to simulate a bootstrap process: + - `custom_components` is reset to properly test the integration + - `async_setup()` method is called + """ + await async_setup(hass, {}) hass.data["custom_components"] = None yield hass diff --git a/tests/test_experiments.py b/tests/test_experiments.py new file mode 100644 index 0000000..1e3ab2f --- /dev/null +++ b/tests/test_experiments.py @@ -0,0 +1,15 @@ +from custom_components.econnect_metronet.binary_sensor import AlertSensor +from custom_components.econnect_metronet.const import DOMAIN + + +class TestExperiments: + def test_sensor_force_update_default(self, coordinator, config_entry, alarm_device): + # Ensure the default is to not force any update + entity = AlertSensor("device_tamper", 7, config_entry, "device_tamper", coordinator, alarm_device) + assert entity._attr_force_update is False + + def test_sensor_force_update_on(self, hass, coordinator, config_entry, alarm_device): + # Ensure you can force the entity update + hass.data[DOMAIN] = {"force_update": True} + entity = AlertSensor("device_tamper", 7, config_entry, "device_tamper", coordinator, alarm_device) + assert entity._attr_force_update is True