Skip to content

Commit

Permalink
Fix high load and hass 2022.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Feb 7, 2022
1 parent 42f0f0c commit 20e89d8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
10 changes: 8 additions & 2 deletions custom_components/miwifi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,22 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
if self._update():
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
self.unsub_update()

self.unsub_update = None

async def async_update(self) -> None:
def _update(self) -> bool:
try:
if self._state == self.luci.api.data["binary_sensor"][self._code]:
return False

self._state = self.luci.api.data["binary_sensor"][self._code]
except KeyError:
self._state = False

return True
41 changes: 31 additions & 10 deletions custom_components/miwifi/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn

add_devices = []
for mac in luci._devices:
add_devices.append(MiWiFiDevice(hass, config_entry, luci, mac, luci._devices[mac], zone_home, False))
add_devices.append(
MiWiFiDevice(hass, config_entry, luci, mac, luci._devices[mac], zone_home, False)
)

if add_devices:
async_add_entities(add_devices)
Expand All @@ -69,7 +71,9 @@ def update_devices() -> None:

_LOGGER.info("New device {} ({}) from {}".format(new_device["name"], mac, luci.api._ip))

new_devices.append(MiWiFiDevice(hass, config_entry, luci, mac, new_device, zone_home))
new_devices.append(
MiWiFiDevice(hass, config_entry, luci, mac, new_device, zone_home)
)
luci.add_device(mac, new_device)

if new_devices:
Expand Down Expand Up @@ -159,6 +163,9 @@ def name(self) -> str:

@property
def icon(self) -> str:
if self._icon:
self._icon.replace("-android", "")

return self._icon

@property
Expand Down Expand Up @@ -229,7 +236,8 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
if self._update():
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
Expand All @@ -241,7 +249,8 @@ async def will_remove_from_hass(self) -> None:
async def async_removed_from_registry(self) -> None:
self.luci.remove_device(self._mac)

async def async_update(self) -> None:
def _update(self) -> bool:
is_changed = False
is_active = False
is_available = False
is_available_all = False
Expand All @@ -254,7 +263,7 @@ async def async_update(self) -> None:
if self._mac in self.hass.data[DOMAIN][entry_id].api._current_devices:
is_active = True

old_router_mac = self._device["router_mac"]
old_device = self._device.copy()
device = self.hass.data[DOMAIN][entry_id].api._devices_list[self._mac]

if "ip" in device and len(device["ip"]) > 0:
Expand All @@ -268,14 +277,18 @@ async def async_update(self) -> None:
self._device["signal"] = device["signal"]
self._device["router_mac"] = device["router_mac"]

if old_router_mac != self._device["router_mac"]:
if old_device["router_mac"] != self._device["router_mac"]:
self.luci.remove_device(self._mac)
remove_enries.append(self._config_entry)

self.luci = self.hass.data[DOMAIN][entry_id]
self._config_entry = self.luci.config_entry

is_available = self.hass.data[DOMAIN][entry_id].available

for field in ["ip", "online", "connection", "signal", "router_mac"]:
if old_device[field] != self._device[field]:
is_changed = True
else:
remove_enries.append(self.hass.data[DOMAIN][entry_id].config_entry)

Expand All @@ -285,17 +298,25 @@ async def async_update(self) -> None:
self.luci.add_device(self._mac, self._device)

for entry in remove_enries:
await self._update_device(self._config_entry, entry)
self._update_device(self._config_entry, entry)
else:
is_available = is_available_all
if self._device["signal"] != 0 and self._device["online"] != 0:
is_changed = True

self._device["signal"] = 0
self._device["online"] = 0

if self._available != is_available or self._active != is_active:
is_changed = True

self._available = is_available
self._active = is_active

async def _update_device(self, new_entry: ConfigEntry, remove_entry: ConfigEntry) -> None:
device_registry = await dr.async_get_registry(self.hass)
return is_changed

def _update_device(self, new_entry: ConfigEntry, remove_entry: ConfigEntry) -> None:
device_registry = dr.async_get(self.hass)

connections = dr._normalize_connections({(dr.CONNECTION_NETWORK_MAC, self._mac)})
device = device_registry.async_get_device({(DOMAIN, self._mac)}, connections)
Expand All @@ -305,7 +326,7 @@ async def _update_device(self, new_entry: ConfigEntry, remove_entry: ConfigEntry

via = device_registry.async_get_device({(DOMAIN, self._device["router_mac"])})

device_registry._async_update_device(
device_registry.async_update_device(
device.id,
add_config_entry_id = new_entry.entry_id,
remove_config_entry_id = remove_entry.entry_id,
Expand Down
12 changes: 9 additions & 3 deletions custom_components/miwifi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,31 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
if self._update():
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
self.unsub_update()

self.unsub_update = None

async def async_update(self) -> None:
def _update(self) -> bool:
if self._is_block:
self._is_block = False

return
return False

try:
if self._state == self.luci.api.data["light"][self._code]:
return False

self._state = self.luci.api.data["light"][self._code]
except KeyError:
self._state = False

return True

async def led_on(self) -> None:
try:
await self.luci.api.led(1)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/miwifi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "miwifi",
"name": "MiWiFi",
"version": "1.3.2",
"version": "1.4.0",
"documentation": "https://github.com/dmamontov/hass-miwifi",
"issue_tracker": "https://github.com/dmamontov/hass-miwifi/issues",
"config_flow": true,
Expand Down
10 changes: 8 additions & 2 deletions custom_components/miwifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,22 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
if self._update():
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
self.unsub_update()

self.unsub_update = None

async def async_update(self) -> None:
def _update(self) -> bool:
try:
if self._state == self.luci.api.data["sensor"][self._code]:
return False

self._state = self.luci.api.data["sensor"][self._code]
except KeyError:
self._state = None

return True
12 changes: 9 additions & 3 deletions custom_components/miwifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,31 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
if self._update():
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
self.unsub_update()

self.unsub_update = None

async def async_update(self) -> None:
def _update(self) -> bool:
if self._is_block:
self._is_block = False

return
return False

try:
if self._state == self.luci.api.data["switch"][self._code]:
return False

self._state = self.luci.api.data["switch"][self._code]
except KeyError:
self._state = False

return True

async def reboot(self) -> None:
try:
await self.luci.api.reboot()
Expand Down

0 comments on commit 20e89d8

Please sign in to comment.