Skip to content

Commit

Permalink
check for os update separated from main query,performed only once per…
Browse files Browse the repository at this point in the history
… 12 hours
  • Loading branch information
tomaae committed Sep 21, 2023
1 parent a490d8a commit 1f937c8
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions custom_components/truenas/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):

self._systemstats_errored = []
self.datasets_hass_device_id = None
self.last_hwinfo_update = datetime(1970, 1, 1)
self.last_updatecheck_update = datetime(1970, 1, 1)

self._is_scale = False
self._is_virtual = False
Expand All @@ -89,10 +89,6 @@ def connected(self) -> str:
# ---------------------------
async def _async_update_data(self):
"""Update TrueNAS data."""
# 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)

await self.hass.async_add_executor_job(self.get_systeminfo)
if self.api.connected():
await self.hass.async_add_executor_job(self.get_systemstats)
Expand All @@ -117,6 +113,11 @@ async def _async_update_data(self):
if self.api.connected():
await self.hass.async_add_executor_job(self.get_app)

delta = datetime.now().replace(microsecond=0) - self.last_updatecheck_update
if self.api.connected() and delta.total_seconds() > 60 * 60 * 12:
await self.hass.async_add_executor_job(self.get_updatecheck)
self.last_updatecheck_update = datetime.now().replace(microsecond=0)

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

Expand Down Expand Up @@ -169,30 +170,6 @@ def get_systeminfo(self) -> None:
if not self.api.connected():
return

self.ds["system_info"] = parse_api(
data=self.ds["system_info"],
source=self.api.query("update/check_available", method="post"),
vals=[
{
"name": "update_status",
"source": "status",
"default": "unknown",
},
{
"name": "update_version",
"source": "version",
"default": "unknown",
},
],
)

if not self.api.connected():
return

self.ds["system_info"]["update_available"] = (
self.ds["system_info"]["update_status"] == "AVAILABLE"
)

if not self.ds["system_info"]["update_available"]:
self.ds["system_info"]["update_version"] = self.ds["system_info"]["version"]

Expand Down Expand Up @@ -282,6 +259,34 @@ def get_systeminfo(self) -> None:
],
)

# ---------------------------
# get_updatecheck
# ---------------------------
def get_updatecheck(self) -> None:
self.ds["system_info"] = parse_api(
data=self.ds["system_info"],
source=self.api.query("update/check_available", method="post"),
vals=[
{
"name": "update_status",
"source": "status",
"default": "unknown",
},
{
"name": "update_version",
"source": "version",
"default": "unknown",
},
],
)

if not self.api.connected():
return

self.ds["system_info"]["update_available"] = (
self.ds["system_info"]["update_status"] == "AVAILABLE"
)

# ---------------------------
# get_systemstats
# ---------------------------
Expand Down

0 comments on commit 1f937c8

Please sign in to comment.