Skip to content

Commit

Permalink
Possible fix for API changes, my dev environment isn't behaving to test
Browse files Browse the repository at this point in the history
  • Loading branch information
magico13 committed Aug 18, 2021
1 parent 6068fad commit a35c890
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
57 changes: 29 additions & 28 deletions custom_components/emporia_vue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
for device in devices:
if not device.device_gid in device_gids:
device_gids.append(device.device_gid)
await loop.run_in_executor(None, vue.populate_device_properties, device)
device_information[device.device_gid] = device
else:
device_information[device.device_gid].channels += device.channels
Expand Down Expand Up @@ -237,37 +236,18 @@ async def update_sensors(vue, scales):
loop = asyncio.get_event_loop()
for scale in scales:
utcnow = datetime.utcnow()
channels = await loop.run_in_executor(
None, vue.get_devices_usage, device_gids, utcnow, scale
usage_dict = await loop.run_in_executor(
None, vue.get_device_list_usage, device_gids, utcnow, scale
)
if not channels:
if not usage_dict:
_LOGGER.warn(
f"No channels found during update for scale {scale}. Retrying..."
)
channels = await loop.run_in_executor(
None, vue.get_devices_usage, device_gids, utcnow, scale
usage_dict = await loop.run_in_executor(
None, vue.get_device_list_usage, device_gids, utcnow, scale
)
if channels:
for channel in channels:
reset_datetime = None
id = make_channel_id(channel, scale)
info = find_device_info_for_channel(channel)
if scale in [Scale.DAY.value, Scale.MONTH.value]:
epochSeconds = channel.timestamp
if epochSeconds:
timestamp = datetime.fromtimestamp(
epochSeconds, timezone.utc
)
reset_datetime = timestamp

data[id] = {
"device_gid": channel.device_gid,
"channel_num": channel.channel_num,
"usage": fix_usage_sign(channel.channel_num, channel.usage),
"scale": scale,
"info": info,
"reset": reset_datetime,
}
if usage_dict:
recurse_usage_data(usage_dict, scale, data)
else:
raise UpdateFailed(f"No channels found during update for scale {scale}")

Expand All @@ -277,6 +257,27 @@ async def update_sensors(vue, scales):
raise UpdateFailed(f"Error communicating with Emporia API: {err}")


def recurse_usage_data(usage_devices, scale, data):
for gid, device in usage_devices.items():
for channel_num, channel in device.channels:
reset_datetime = None
id = make_channel_id(channel, scale)
info = find_device_info_for_channel(channel)
if scale in [Scale.DAY.value, Scale.MONTH.value]:
reset_datetime = device.timestamp

data[id] = {
"device_gid": gid,
"channel_num": channel_num,
"usage": fix_usage_sign(channel_num, channel.usage),
"scale": scale,
"info": info,
"reset": reset_datetime,
}
if channel.nested_devices:
recurse_usage_data(channel.nested_devices, scale, data)


def find_device_info_for_channel(channel):
info = None
if channel.device_gid in device_information:
Expand All @@ -297,7 +298,7 @@ def find_device_info_for_channel(channel):
info.channels.append(
VueDeviceChannel(
gid=channel.device_gid,
name=None,
name=channel.name,
channelNum=channel.channel_num,
channelMultiplier=channel_123.channel_multiplier,
channelTypeGid=channel_123.channel_type_gid,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/emporia_vue/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Emporia Vue",
"config_flow": true,
"documentation": "https://github.com/magico13/ha-emporia-vue",
"requirements": ["pyemvue==0.12.4"],
"requirements": ["pyemvue==0.14.0"],
"ssdp": [],
"zeroconf": [],
"homekit": {},
Expand Down
7 changes: 4 additions & 3 deletions custom_components/emporia_vue/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def __init__(self, coordinator, id):
raise RuntimeError(
f"No channel found for device_gid {device_gid} and channel_num {channel_num}"
)

dName = self._channel.name or self._device.device_name
self._name = f"Power {dName} {self._channel.channel_num} {self._scale}"
dName = self._device.name
if self._channel.name and self._channel.name != "Main":
dName = self._channel.name
self._name = f"Power {dName} {channel_num} {self._scale}"
self._iskwh = self.scale_is_energy()

@property
Expand Down

0 comments on commit a35c890

Please sign in to comment.