Skip to content

Commit

Permalink
Added coordinator file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Wieskamp committed Feb 9, 2024
1 parent ea39b9f commit a3f86df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions custom_components/glentronics/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import annotations

import aiohttp
import json
import pydash
from datetime import timedelta
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed
)
from homeassistant.const import CONF_USERNAME, CONF_PIN
from .const import DOMAIN, _LOGGER, UPDATE_FREQ, LOGIN_URL, API_URL, API_USERNAME, API_PASSWORD

class GlentronicsCoordinator(DataUpdateCoordinator):

def __init__(self, hass, config):
self.login_url = LOGIN_URL
self.api_url = API_URL
self.creds = {
"APIUsername": API_USERNAME,
"APIPassword": API_PASSWORD,
"ProxyID": config.data[CONF_PIN],
"Username": config.data[CONF_USERNAME]
}

super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=timedelta(seconds=UPDATE_FREQ)
)

async def _async_update_data(self):
try:
url = API_URL + "/Device/RetrieveProxyStatus"
async with aiohttp.ClientSession() as session:
async with session.post(url,data=self.creds) as r:
results = await r.json()
device = []
device.append(pydash.get(results,"Location"))
device.append(pydash.get(results,"StatusList.0.ControlUnitType"))
self.device = device
self.fields = pydash.get(results,"StatusFields")
except Exception as err:
_LOGGER.error(f"Error communicating with API: {err}")
raise UpdateFailed(err)
2 changes: 1 addition & 1 deletion custom_components/glentronics/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"issue_tracker": "https://github.com/theOrakle/glentronics/issues",
"loggers": ["custom_components.glentronics"],
"requirements": ["pydash"],
"version": "0.1.5",
"version": "0.1.6",
"dependencies": [],
"codeowners": ["@theOrakle"]
}

0 comments on commit a3f86df

Please sign in to comment.