From ffacae1ca4494d24df69c0a2d60783b52c85c9f5 Mon Sep 17 00:00:00 2001 From: Ryan Winter Date: Fri, 29 Apr 2022 16:37:18 -0700 Subject: [PATCH] fallback to using InstantaneousDemand reading from device if DeviceInfo fails --- .../rainforest_emu_2/__init__.py | 12 ++-- .../rainforest_emu_2/config_flow.py | 67 ++++++++++--------- .../rainforest_emu_2/manifest.json | 2 +- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/custom_components/rainforest_emu_2/__init__.py b/custom_components/rainforest_emu_2/__init__.py index 45d5e9b..59865a2 100644 --- a/custom_components/rainforest_emu_2/__init__.py +++ b/custom_components/rainforest_emu_2/__init__.py @@ -68,12 +68,10 @@ def __init__( self._callbacks = set() self._power = None - + self._summation_delivered = None self._summation_received = None - self._current_price = None - self._current_usage = None self._current_usage_start_date = dt.utc_from_timestamp(0) @@ -133,19 +131,19 @@ def device_name(self) -> str: @property def device_manufacturer(self) -> str: - return self._properties[ATTR_MANUFACTURER] + self._properties.get(ATTR_MANUFACTURER) @property def device_model(self) -> str: - return self._properties[ATTR_MODEL] + self._properties.get(ATTR_MODEL) @property def device_sw_version(self) -> str: - return self._properties[ATTR_SW_VERSION] + self._properties.get(ATTR_SW_VERSION) @property def device_hw_version(self) -> str: - return self._properties[ATTR_HW_VERSION] + self._properties.get(ATTR_HW_VERSION) @property def power(self) -> float: diff --git a/custom_components/rainforest_emu_2/config_flow.py b/custom_components/rainforest_emu_2/config_flow.py index 4b37818..00eed53 100644 --- a/custom_components/rainforest_emu_2/config_flow.py +++ b/custom_components/rainforest_emu_2/config_flow.py @@ -25,7 +25,8 @@ ) from .emu2 import Emu2 from .emu2_entities import ( - DeviceInfo + DeviceInfo, + InstantaneousDemand ) _LOGGER = logging.getLogger(__name__) @@ -63,15 +64,9 @@ async def async_step_user(self, user_input = None): usb.get_serial_by_id, port.device ) - device_properties = await self.get_device_properties(device_path) + device_properties = await self.async_get_device_properties(device_path) if device_properties is not None: - await self.async_set_unique_id(device_properties[ATTR_DEVICE_MAC_ID]) - self._abort_if_unique_id_configured() - - return self.async_create_entry( - title = device_path, - data = device_properties - ) + return await self.async_setup_device(device_path, device_properties) _LOGGER.info("EMU-2 device not detected on %s", device_path) errors[CONF_DEVICE_PATH] = "not_detected" @@ -83,7 +78,6 @@ async def async_step_user(self, user_input = None): ) return self.async_show_form(step_id="user", data_schema = schema, errors = errors) - async def async_step_manual(self, user_input = None): """Manually specify the path.""" errors = {} @@ -91,15 +85,9 @@ async def async_step_manual(self, user_input = None): if user_input is not None: device_path = user_input[CONF_DEVICE_PATH] - device_properties = await self.get_device_properties(device_path) + device_properties = await self.async_get_device_properties(device_path) if device_properties is not None: - await self.async_set_unique_id(device_properties[ATTR_DEVICE_MAC_ID]) - self._abort_if_unique_id_configured() - - return self.async_create_entry( - title = device_path, - data = device_properties - ) + return await self.async_setup_device(device_path, device_properties) errors[CONF_DEVICE_PATH] = "not_detected" @@ -110,7 +98,16 @@ async def async_step_manual(self, user_input = None): ) return self.async_show_form(step_id = "manual", data_schema = schema, errors = errors) - async def get_device_properties(self, device_path: str) -> dict[str, str]: + async def async_setup_device(self, device_path: str, device_properties: dict): + await self.async_set_unique_id(device_properties[ATTR_DEVICE_MAC_ID]) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title = device_path, + data = device_properties + ) + + async def async_get_device_properties(self, device_path: str) -> dict[str, str]: """Probe the the device for the its properties.""" emu2 = Emu2(device_path) @@ -135,16 +132,24 @@ async def get_device_properties(self, device_path: str) -> dict[str, str]: await emu2.close() response = emu2.get_data(DeviceInfo) + if response is not None: + return { + ATTR_DEVICE_PATH: device_path, + ATTR_DEVICE_MAC_ID: response.device_mac, + ATTR_SW_VERSION: response.fw_version, + ATTR_HW_VERSION: response.hw_version, + ATTR_MANUFACTURER: response.manufacturer, + ATTR_MODEL: response.model_id + } + _LOGGER.debug("get_devices_properties DeviceInfo response is None") + + # For some reason we didnt get a DeviceInfo response, failback to an InstananeousDemand response + response = emu2.get_data(InstantaneousDemand) + if response is not None: + return { + ATTR_DEVICE_PATH: device_path, + ATTR_DEVICE_MAC_ID: response.device_mac, + } - if response is None: - _LOGGER.debug("get_devices_properties response is None") - return None - - return { - ATTR_DEVICE_PATH: device_path, - ATTR_DEVICE_MAC_ID: response.device_mac, - ATTR_SW_VERSION: response.fw_version, - ATTR_HW_VERSION: response.hw_version, - ATTR_MANUFACTURER: response.manufacturer, - ATTR_MODEL: response.model_id - } + _LOGGER.debug("get_devices_properties InstantaneousDemand response is None") + return None diff --git a/custom_components/rainforest_emu_2/manifest.json b/custom_components/rainforest_emu_2/manifest.json index 0b07802..6a9505e 100644 --- a/custom_components/rainforest_emu_2/manifest.json +++ b/custom_components/rainforest_emu_2/manifest.json @@ -9,7 +9,7 @@ "pyserial-asyncio==0.6" ], "iot_class": "local_polling", - "version": "1.1.6", + "version": "1.2.0", "config_flow": true, "usb": [ {