Skip to content

Commit

Permalink
Cleanup & Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Wieskamp committed Feb 14, 2024
1 parent a3f86df commit a750e96
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 35 deletions.
7 changes: 2 additions & 5 deletions custom_components/glentronics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
from homeassistant.const import Platform
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from .const import DOMAIN
from .coordinator import GlentronicsCoordinator
from .coordinator import MyCoordinator

PLATFORMS = [
Platform.BINARY_SENSOR
]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = GlentronicsCoordinator(hass, entry)
coordinator = MyCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

return unload_ok
18 changes: 7 additions & 11 deletions custom_components/glentronics/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,37 @@
from .const import DOMAIN, _LOGGER, BINARY_SENSORS

async def async_setup_entry(hass, config, async_add_entities):

coordinator = hass.data[DOMAIN][config.entry_id]
entities = []
for field in BINARY_SENSORS:
entities.append(GlentronicsSensor(coordinator, field, BINARY_SENSORS[field]))
entities.append(MySensor(coordinator, field, BINARY_SENSORS[field]))
async_add_entities(entities)

class GlentronicsSensor(Entity):

class MySensor(Entity):
def __init__(self,coordinator,idx,entity):
self.coordinator = coordinator
self.entity = entity
self.idx = idx
self.device = self.coordinator.device[0]
self.device = self.coordinator.name
self.field = entity.name
self._unique_id = f"{DOMAIN}_{self.device}_{self.field}"
self._name = f"{self.device}_{self.field}"
self._icon = entity.icon
self._device_class = entity.device_class
self._state = None
self._attributes = {}
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.device)},
manufacturer=DOMAIN,
model=self.coordinator.device[1],
model=pydash.get(self.coordinator.device,"ControlUnitType"),
sw_version=pydash.get(self.coordinator.device,"FirmwareVersion"),
name=self.device.capitalize())

@property
def unique_id(self):
return self._unique_id
return f"{DOMAIN}_{self.device}_{self.field}"

@property
def name(self):
return self._name
return f"{self.device}_{self.field}"

@property
def icon(self):
Expand All @@ -56,7 +53,6 @@ def state_attributes(self):
return self._attributes

async def async_update(self) -> None:

for field in self.coordinator.fields:
if pydash.get(field,"FieldLabel").find(self.idx) == 0:
state = pydash.get(field,"FieldStatusOK")
Expand Down
2 changes: 0 additions & 2 deletions custom_components/glentronics/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ async def validate_input(hass: core.HomeAssistant, data):
raise AuthenticationError()

class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1

async def async_step_user(self, user_input=None):
errors = {}
if user_input is not None:
Expand Down
5 changes: 0 additions & 5 deletions custom_components/glentronics/const.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""Constants for the Glentronics online component."""
import logging

from homeassistant.const import Platform
from homeassistant.components.binary_sensor import BinarySensorDeviceClass, BinarySensorEntityDescription

_LOGGER = logging.getLogger(__name__)

DOMAIN = "glentronics"

COORDINATOR = "coordinator"

# In Seconds
UPDATE_FREQ = 30

Expand All @@ -18,8 +15,6 @@
API_USERNAME = "Glentronics"
API_PASSWORD = "API201622@"

PLATFORMS = [Platform.BINARY_SENSOR]

BINARY_SENSORS = {
"Alarm Status": BinarySensorEntityDescription(
name="Status",
Expand Down
14 changes: 4 additions & 10 deletions custom_components/glentronics/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
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):
from .const import DOMAIN, _LOGGER, UPDATE_FREQ, API_URL, API_USERNAME, API_PASSWORD

class MyCoordinator(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,
Expand All @@ -37,10 +33,8 @@ async def _async_update_data(self):
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.name = pydash.get(results,"Location")
self.device = pydash.get(results,"StatusList.0")
self.fields = pydash.get(results,"StatusFields")
except Exception as err:
_LOGGER.error(f"Error communicating with API: {err}")
Expand Down
1 change: 0 additions & 1 deletion custom_components/glentronics/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
class ApiException(Exception):
"""General API exception."""


class AuthenticationError(ApiException):
"""To indicate there is an issue authenticating."""
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.6",
"version": "0.1.7",
"dependencies": [],
"codeowners": ["@theOrakle"]
}

0 comments on commit a750e96

Please sign in to comment.