Skip to content

Commit

Permalink
Replaced all coordinator with only one (#22)
Browse files Browse the repository at this point in the history
* Replaced all coordinator with only one

* Add isort in quality check
  • Loading branch information
arjenbos authored Nov 2, 2023
1 parent 00637bb commit 4ed6284
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 269 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ jobs:
# - name: pytest
# run: |
# pylint $(git ls-files '*.py')
isort:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v3"
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: 'pip'
cache-dependency-path: |
**/setup.cfg
**/requirements*.txt
- name: Install dependencies
run: |
pip install -r requirements-test.txt
- name: run
run: |
isort custom_components/alpha_innotec tests --check-only
test:
runs-on: "ubuntu-latest"
needs:
- isort
steps:
- uses: "actions/checkout@v3"
- name: Set up Python 3.11
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alpha_innotec/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import base64
import logging

from backports.pbkdf2 import pbkdf2_hmac
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from backports.pbkdf2 import pbkdf2_hmac

_LOGGER = logging.getLogger(__name__)

Expand Down
64 changes: 0 additions & 64 deletions custom_components/alpha_innotec/base_coordinator.py

This file was deleted.

86 changes: 13 additions & 73 deletions custom_components/alpha_innotec/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,52 @@
import logging
from datetime import timedelta

from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorEntityDescription, \
BinarySensorDeviceClass
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.typing import UndefinedType
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.helpers.update_coordinator import (CoordinatorEntity,
DataUpdateCoordinator)

from .const import DOMAIN, MANUFACTURER
from .coordinator import AlphaInnotecCoordinator
from .gateway_api import GatewayAPI
from .structs.Valve import Valve

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, entry, async_add_entities):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
"""Set up the sensor platform."""

_LOGGER.debug("Setting up binary sensors")

gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api']

coordinator = AlphaInnotecBinarySensorCoordinator(hass, gateway_api)
coordinator = AlphaInnotecCoordinator(hass)

await coordinator.async_config_entry_first_refresh()

entities = []

for valve in coordinator.data:
for valve in coordinator.data['valves']:
entities.append(AlphaHomeBinarySensor(
coordinator=coordinator,
name=valve.name,
description=BinarySensorEntityDescription("", entity_registry_enabled_default=valve.used),
valve=valve
))

async_add_entities(entities)


class AlphaInnotecBinarySensorCoordinator(DataUpdateCoordinator):
"""My custom coordinator."""

data: list[Valve]

def __init__(self, hass: HomeAssistant, gateway_api: GatewayAPI):
"""Initialize my coordinator."""
super().__init__(
hass,
_LOGGER,
name="Alpha Innotec Binary Coordinator",
update_interval=timedelta(seconds=30),
)

self.gateway_api: GatewayAPI = gateway_api

async def _async_update_data(self) -> list[Valve]:
"""Fetch data from API endpoint."""

db_modules: dict = await self.hass.async_add_executor_job(self.gateway_api.db_modules)
all_modules: dict = await self.hass.async_add_executor_job(self.gateway_api.all_modules)

valves: list[Valve] = []

for module_id in db_modules["modules"]:
module = db_modules["modules"][module_id]

if module["productId"] != 3:
continue

for instance in module["instances"]:
valve_id = '0' + instance['instance'] + module['deviceid'][2:]

used = False

for room_id in all_modules:
if used is not True:
used = valve_id in all_modules[room_id]["modules"]

valve = Valve(
identifier=valve_id,
name=module["name"] + '-' + instance['instance'],
instance=instance["instance"],
device_id=module["deviceid"],
device_name=module["name"],
status=instance["status"],
used=used
)

valves.append(valve)

_LOGGER.debug("Finished getting valves from API")

return valves


class AlphaHomeBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of a Binary Sensor."""

def __init__(self, coordinator: AlphaInnotecBinarySensorCoordinator, name: str, description: BinarySensorEntityDescription, valve: Valve) -> None:
def __init__(self, coordinator: AlphaInnotecCoordinator, description: BinarySensorEntityDescription, valve: Valve) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator, context=valve.identifier)
self.entity_description = description
self._attr_name = name
self.valve = valve
self._attr_is_on = valve.status

@property
def device_info(self) -> DeviceInfo:
Expand All @@ -125,7 +64,7 @@ def device_info(self) -> DeviceInfo:

@property
def name(self) -> str | UndefinedType | None:
return self._attr_name
return self.valve.name

@property
def unique_id(self) -> str:
Expand All @@ -142,10 +81,11 @@ def device_class(self) -> BinarySensorDeviceClass | None:

@callback
def _handle_coordinator_update(self) -> None:
for valve in self.coordinator.data:
for valve in self.coordinator.data['valves']:
if valve.identifier == self.valve.identifier:
self.valve = valve
break

_LOGGER.debug("Updating binary sensor: %s", self.valve.identifier)

self.async_write_ha_state()
Loading

1 comment on commit 4ed6284

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HA Alpha Innotec Coverage

HA Alpha Innotec
FileStmtsMissCoverMissing
__init__.py21957%19–20, 22, 27, 29, 34, 36–37, 39
api.py44295%48, 51
binary_sensor.py50500%2, 4–5, 7, 9–13, 16–19, 21, 24, 27, 29, 31, 33, 35–36, 42, 45, 48, 50–52, 54–55, 57, 65–67, 69–70, 72, 74–76, 78–80, 82–87, 89, 91
climate.py62620%2, 4–5, 7, 11–16, 18–20, 22, 25, 28, 30, 32, 34, 36–37, 43, 46, 49–51, 55, 57–59, 61–62, 64, 72–73, 75, 77–79, 81–82, 84–86, 88, 90, 93–94, 96, 98–99, 101, 103, 105–107, 109–110, 112, 114–115, 118
config_flow.py371559%24–25, 27–30, 32–35, 57–61
const.py70100% 
controller_api.py786121%17–18, 20–21, 23–26, 28, 30–33, 35, 38, 40, 43, 45, 50, 52–55, 57, 59–60, 62, 64, 71, 73, 75, 82, 84–85, 87, 89, 91, 93, 95–96, 98, 101, 104–105, 107–110, 112, 115, 118, 124, 126–130, 142–144, 146
coordinator.py49490%1–2, 4–6, 8–11, 13, 16, 18, 20, 22, 29–30, 32–35, 37–38, 40–42, 44–45, 47–49, 51, 53–55, 57, 69, 71–72, 74–75, 77–78, 80, 82–84, 86, 96, 98
gateway_api.py604525%17–19, 21–23, 26–27, 29–30, 32–35, 37, 39–42, 44, 47, 49, 52, 54, 59, 61, 63–65, 67, 69–70, 72, 74, 77, 79–80, 82, 85, 87, 90, 93–95, 97
sensor.py49490%2, 4–6, 8, 10–14, 16–18, 20, 23, 26, 28, 30, 32, 34–35, 41, 44, 47–48, 50, 53–55, 57–58, 60, 68–69, 71, 73–75, 77–79, 81–86, 88, 90
structs
   Thermostat.py11918%8–16
TOTAL46835125% 

Tests Skipped Failures Errors Time
3 0 💤 0 ❌ 0 🔥 0.573s ⏱️

Please sign in to comment.