Skip to content

Commit

Permalink
feat(experimental): add force_update as experimental setting in YAM…
Browse files Browse the repository at this point in the history
…L conf (#85)

#85
  • Loading branch information
palazzem authored Oct 18, 2023
1 parent 5c68a26 commit 7aaeb37
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
13 changes: 9 additions & 4 deletions custom_components/econnect_metronet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
9 changes: 9 additions & 0 deletions custom_components/econnect_metronet/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions tests/test_experiments.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7aaeb37

Please sign in to comment.