Skip to content

Commit

Permalink
merged hwinfo and fwupdate_check into main update loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Aug 9, 2023
1 parent b8f4856 commit c83e735
Showing 1 changed file with 37 additions and 64 deletions.
101 changes: 37 additions & 64 deletions custom_components/mikrotik_router/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,8 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
self.async_mac_lookup = AsyncMacLookup()
self.accessrights_reported = False

# self.listeners.append(
# async_track_time_interval(
# self.hass, self.force_fwupdate_check, timedelta(hours=4)
# )
# )
self.last_hwinfo_update = datetime(1970, 1, 1)

# self.listeners.append(
# async_track_time_interval(
# self.hass, self.async_ping_tracked_hosts, timedelta(seconds=15)
Expand All @@ -214,11 +211,6 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
# async def async_init(self):
# self.listeners.append(
# async_track_time_interval(
# self.hass, self.force_fwupdate_check, timedelta(hours=4)
# )
# )
# self.listeners.append(
# async_track_time_interval(
# self.hass, self.async_ping_tracked_hosts, timedelta(seconds=15)
# )
# )
Expand Down Expand Up @@ -475,56 +467,6 @@ async def async_get_host_hass(self):

self.data["host_hass"][tmp[2].upper()] = entity.original_name

# ---------------------------
# async_hwinfo_update
# ---------------------------
async def async_hwinfo_update(self):
"""Update Mikrotik hardware info"""
# try:
# await asyncio.wait_for(self.lock.acquire(), timeout=30)
# except Exception as error:
# raise UpdateFailed(error) from error

await self.hass.async_add_executor_job(self.get_access)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_firmware_update)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_system_resource)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_capabilities)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_system_routerboard)

if self.api.connected() and self.option_sensor_scripts:
await self.hass.async_add_executor_job(self.get_script)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_dhcp_network)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_dns)

# self.lock.release()

# ---------------------------
# force_fwupdate_check
# ---------------------------
@callback
async def force_fwupdate_check(self, _now=None):
"""Trigger hourly update by timer"""
await self.async_fwupdate_check()

# ---------------------------
# async_fwupdate_check
# ---------------------------
async def async_fwupdate_check(self):
"""Update Mikrotik data"""
await self.hass.async_add_executor_job(self.get_firmware_update)

# ---------------------------
# async_ping_tracked_hosts
# ---------------------------
Expand Down Expand Up @@ -591,8 +533,36 @@ async def async_ping_tracked_hosts(self, _now=None):
# ---------------------------
async def _async_update_data(self):
"""Update Mikrotik data"""
if self.api.has_reconnected():
await self.async_hwinfo_update()
delta = datetime.now().replace(microsecond=0) - self.last_hwinfo_update
if self.api.has_reconnected() or delta.total_seconds() > 60 * 60 * 4:
await self.hass.async_add_executor_job(self.get_access)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_firmware_update)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_system_resource)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_capabilities)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_system_routerboard)

if self.api.connected() and self.option_sensor_scripts:
await self.hass.async_add_executor_job(self.get_script)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_dhcp_network)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_dns)

if not self.api.connected():
raise UpdateFailed("Mikrotik Disconnected")

if self.api.connected():
self.last_hwinfo_update = datetime.now().replace(microsecond=0)

# try:
# await asyncio.wait_for(self.lock.acquire(), timeout=10)
Expand All @@ -601,8 +571,8 @@ async def _async_update_data(self):

await self.hass.async_add_executor_job(self.get_system_resource)

if self.api.connected() and "available" not in self.data["fw-update"]:
await self.async_fwupdate_check()
# if self.api.connected() and "available" not in self.data["fw-update"]:
# await self.hass.async_add_executor_job(self.get_firmware_update)

if self.api.connected():
await self.hass.async_add_executor_job(self.get_system_health)
Expand Down Expand Up @@ -676,6 +646,9 @@ async def _async_update_data(self):
if self.api.connected() and self.support_gps:
await self.hass.async_add_executor_job(self.get_gps)

if not self.api.connected():
raise UpdateFailed("Mikrotik Disconnected")

# self.lock.release()
# async_dispatcher_send(self.hass, "update_sensors", self)
return self.data
Expand Down

0 comments on commit c83e735

Please sign in to comment.