Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from ryanwinter/device_not_respond
Browse files Browse the repository at this point in the history
Device not responding during setup
  • Loading branch information
ryanwinter authored Apr 30, 2022
2 parents 22a7369 + bec837e commit d6d5d2e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
12 changes: 5 additions & 7 deletions custom_components/rainforest_emu_2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
67 changes: 36 additions & 31 deletions custom_components/rainforest_emu_2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
)
from .emu2 import Emu2
from .emu2_entities import (
DeviceInfo
DeviceInfo,
InstantaneousDemand
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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"
Expand All @@ -83,23 +78,16 @@ 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 = {}

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"

Expand All @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion custom_components/rainforest_emu_2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"pyserial-asyncio==0.6"
],
"iot_class": "local_polling",
"version": "1.1.6",
"version": "1.2.0",
"config_flow": true,
"usb": [
{
Expand Down

0 comments on commit d6d5d2e

Please sign in to comment.