From f9b8ef2e01292be30b430cc0520e876794e89053 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 1 Aug 2022 20:03:32 +0200 Subject: [PATCH] Do not process an unknown state updates (#7) --- custom_components/elro_connects/__init__.py | 12 +++++------- custom_components/elro_connects/device.py | 6 ++++-- custom_components/elro_connects/manifest.json | 2 +- requirements_dev.txt | 2 +- requirements_test.txt | 2 +- tests/test_init.py | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/custom_components/elro_connects/__init__.py b/custom_components/elro_connects/__init__.py index 67207a7..2f57379 100644 --- a/custom_components/elro_connects/__init__.py +++ b/custom_components/elro_connects/__init__.py @@ -6,7 +6,7 @@ import logging from elro.api import K1 -from elro.device import ATTR_DEVICE_STATE, STATE_OFFLINE, STATE_UNKNOWN +from elro.device import ATTR_DEVICE_STATE, STATE_UNKNOWN from homeassistant.config_entries import ConfigEntry from homeassistant.const import SERVICE_RELOAD, Platform @@ -31,9 +31,9 @@ async def _async_update_data() -> dict[int, dict]: nonlocal current_device_set # get state from coordinator cash in case the current state is unknown coordinator_update: dict[int, dict] = copy.deepcopy(coordinator.data or {}) - # set initial state to offline + # set initial state to unknown for device_id, state_base in coordinator_update.items(): - state_base[ATTR_DEVICE_STATE] = STATE_OFFLINE + state_base[ATTR_DEVICE_STATE] = STATE_UNKNOWN try: await hass.async_create_task(elro_connects_api.async_update()) device_update = copy.deepcopy(elro_connects_api.data) @@ -45,10 +45,8 @@ async def _async_update_data() -> dict[int, dict]: # new device, or known state coordinator_update[device_id] = device_data elif device_data[ATTR_DEVICE_STATE] == STATE_UNKNOWN: - # update device state only, other data is not valid - coordinator_update[device_id][ATTR_DEVICE_STATE] = device_data[ - ATTR_DEVICE_STATE - ] + # do not process unknown state updates + continue else: # update full state coordinator_update[device_id] = device_data diff --git a/custom_components/elro_connects/device.py b/custom_components/elro_connects/device.py index 890d146..486be2d 100644 --- a/custom_components/elro_connects/device.py +++ b/custom_components/elro_connects/device.py @@ -16,7 +16,7 @@ from elro.utils import update_state_data from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_NAME, CONF_HOST, CONF_PORT +from homeassistant.const import ATTR_NAME, CONF_API_KEY, CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr from homeassistant.helpers.entity import DeviceInfo, EntityDescription @@ -80,7 +80,9 @@ async def async_update_settings( self, hass: HomeAssistant, entry: ConfigEntry ) -> None: """Process updated settings.""" - await self.async_configure(entry.data[CONF_HOST], entry.data[CONF_PORT]) + await self.async_configure( + entry.data[CONF_HOST], entry.data[CONF_PORT], entry.data.get(CONF_API_KEY) + ) @property def data(self) -> dict[int, dict]: diff --git a/custom_components/elro_connects/manifest.json b/custom_components/elro_connects/manifest.json index 163b59a..81580ab 100644 --- a/custom_components/elro_connects/manifest.json +++ b/custom_components/elro_connects/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://github.com/jbouwh/ha-elro-connects", "issue_tracker": "https://github.com/jbouwh/ha-elro-connects/issues", - "requirements": ["lib-elro-connects==0.5.1"], + "requirements": ["lib-elro-connects==0.5.2"], "codeowners": ["@jbouwh"], "iot_class": "local_polling", "version": "0.2.0" diff --git a/requirements_dev.txt b/requirements_dev.txt index 8d28dfb..5fd28c8 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,4 +1,4 @@ pre-commit -lib-elro-connects==0.5.1 +lib-elro-connects==0.5.2 homeassistant pytest-homeassistant-custom-component diff --git a/requirements_test.txt b/requirements_test.txt index b82e023..bb6fb38 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -26,6 +26,6 @@ fnvhash==0.1.0 lru-dict==1.1.7 -lib-elro-connects==0.5.1 +lib-elro-connects==0.5.2 pytest-homeassistant-custom-component diff --git a/tests/test_init.py b/tests/test_init.py index 53643f9..614c416 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -110,6 +110,6 @@ async def test_configure_platforms_dynamically( async_fire_time_changed(hass, time) await hass.async_block_till_done() - assert hass.states.get("siren.beganegrond").state == "unknown" + assert hass.states.get("siren.beganegrond").state == "off" assert hass.states.get("siren.eerste_etage") is not None assert hass.states.get("siren.zolder") is not None