Skip to content

Commit

Permalink
fix: update entity state after network discovery (#1291) (#1295)
Browse files Browse the repository at this point in the history
Previously, each platform forced a refresh on the coordinator. This was slow and error prone. Now, when the network is discovered full entity state is discovered as well.
Fixes #1289

Co-authored-by: Brady Mulhollem <[email protected]>
  • Loading branch information
alandtse and blm126 authored May 15, 2021
2 parents 44071bd + 9804dd3 commit b2302ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
14 changes: 13 additions & 1 deletion custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ async def async_update_data() -> Optional[AlexaEntityData]:
should_get_network = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"should_get_network"
]
extended_entity_discovery = hass.data[DATA_ALEXAMEDIA]["accounts"][email][
"options"
].get(CONF_EXTENDED_ENTITY_DISCOVERY)

devices = {}
bluetooth = {}
Expand Down Expand Up @@ -434,7 +437,16 @@ async def async_update_data() -> Optional[AlexaEntityData]:
"should_get_network"
] = False

if entities_to_monitor:
# First run is a special case. Get the state of all entities(including disabled)
# This ensures all entities have state during startup without needing to request coordinator refresh
for typeOfEntity, entities in alexa_entities.items():
if typeOfEntity == "guard" or extended_entity_discovery:
for entity in entities:
entities_to_monitor.add(entity.get("id"))
entity_state = await get_entity_data(
login_obj, list(entities_to_monitor)
)
elif entities_to_monitor:
entity_state = optional_task_results.pop()

if new_devices:
Expand Down
3 changes: 0 additions & 3 deletions custom_components/alexa_media/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ async def async_setup_platform(hass, config, add_devices_callback, discovery_inf
le["name"],
)

if devices:
await coordinator.async_refresh()

return await add_devices(
hide_email(account),
devices,
Expand Down
1 change: 0 additions & 1 deletion custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ async def create_temperature_sensors(account_dict, temperature_entities):
account_dict["entities"]["sensor"].setdefault(serial, {})
account_dict["entities"]["sensor"][serial]["Temperature"] = sensor
devices.append(sensor)
await coordinator.async_request_refresh()
return devices


Expand Down

0 comments on commit b2302ec

Please sign in to comment.