Skip to content

Commit

Permalink
fix: multiple device updating incorrectly and race condition on devic…
Browse files Browse the repository at this point in the history
…e connection
  • Loading branch information
humbertogontijo committed Aug 3, 2023
1 parent c037338 commit d778c1d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
13 changes: 8 additions & 5 deletions custom_components/roborock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
else home_data.devices
)
if not cloud_integration:
devices_without_ip = [_device for _device in devices if _device.duid not in device_network.keys()]
devices_without_ip = [_device for _device in devices if _device.duid not in device_network]
if len(devices_without_ip) > 0:
device_network.update(await get_local_devices_info())
for _device in devices:
Expand All @@ -110,7 +110,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
network = device_network.get(device_id)
if network is None:
networking = await map_client.get_networking()
network = {"ip": networking.ip}
network = DeviceNetwork(ip=networking.ip, mac="")
device_network[device_id] = network
hass.config_entries.async_update_entry(
entry, data={"device_network": device_network, **data}
)
Expand All @@ -122,7 +123,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
data_coordinator = RoborockDataUpdateCoordinator(
hass, main_client, map_client, device_info, home_data.rooms
)
map_client.add_listener(lambda attr, _data: data_coordinator.update_device(attr, _data))
map_client.add_listener(data_coordinator.update_device)
path = Path(hass.config.path(STORAGE_PATH.format(key=f"{DOMAIN}.{entry.entry_id}.{slugify(device_id)}")))
devices_entry_data[device_id] = {
"coordinator": data_coordinator,
Expand All @@ -132,8 +133,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.warning(f"Failing setting up device {device_id}")

await asyncio.gather(
*(device_entry_data["coordinator"].async_config_entry_first_refresh() for device_entry_data in
devices_entry_data.values()),
*(
device_entry_data["coordinator"].async_config_entry_first_refresh()
for device_entry_data in devices_entry_data.values()
),
return_exceptions=True
)

Expand Down
7 changes: 4 additions & 3 deletions custom_components/roborock/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ async def fill_device_prop(self, device_info: RoborockHassDeviceInfo) -> None:
else:
device_info.props = device_prop

def update_device(self, attribute: str, data: RoborockBase):
def update_device(self, device_id: str, attribute: str, data: RoborockBase):
"""Update device based on prop attribute."""
setattr(self.device_info.props, attribute, data)
self.hass.loop.call_soon_threadsafe(self.async_set_updated_data, self.device_info)
if device_id == self.device_info.device.duid:
setattr(self.device_info.props, attribute, data)
self.hass.loop.call_soon_threadsafe(self.async_set_updated_data, self.device_info)

async def fill_room_mapping(self, device_info: RoborockHassDeviceInfo) -> None:
"""Build the room mapping - only works for local api."""
Expand Down
4 changes: 2 additions & 2 deletions custom_components/roborock/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"roborock"
],
"requirements": [
"python-roborock==0.30.2",
"python-roborock==0.32.0",
"ical==5.0.0"
],
"version": "1.0.9"
"version": "1.0.10"
}
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
homeassistant==2023.7.3
voluptuous==0.13.1
python_roborock==0.30.2
python_roborock==0.32.0
Pillow==10.0.0
ruff==0.0.280
ruff==0.0.282
ical==5.0.0

0 comments on commit d778c1d

Please sign in to comment.