Skip to content

Commit

Permalink
Update Battery Sensor and Binary Sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenbos committed Oct 31, 2023
1 parent b715514 commit a1ea6ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
27 changes: 17 additions & 10 deletions custom_components/alpha_innotec/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorEntityDescription, \
BinarySensorDeviceClass
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.typing import UndefinedType
from homeassistant.helpers.update_coordinator import (
Expand All @@ -28,7 +28,7 @@ async def async_setup_entry(hass, entry, async_add_entities):

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

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

await coordinator.async_config_entry_first_refresh()

Expand All @@ -45,7 +45,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
async_add_entities(entities)


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

def __init__(self, hass: HomeAssistant, gateway_api: GatewayAPI):
Expand All @@ -60,11 +60,7 @@ def __init__(self, hass: HomeAssistant, gateway_api: GatewayAPI):
self.gateway_api: GatewayAPI = gateway_api

async def _async_update_data(self) -> list[Valve]:
"""Fetch data from API endpoint.
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
"""Fetch data from API endpoint."""

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

Expand Down Expand Up @@ -94,14 +90,15 @@ async def _async_update_data(self) -> list[Valve]:


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

def __init__(self, coordinator: AlphaCoordinator, name: str, description: BinarySensorEntityDescription, valve: Valve) -> None:
def __init__(self, coordinator: AlphaInnotecBinarySensorCoordinator, name: str, 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 Down Expand Up @@ -130,3 +127,13 @@ def is_on(self) -> bool | None:
@property
def device_class(self) -> BinarySensorDeviceClass | None:
return BinarySensorDeviceClass.OPENING

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

_LOGGER.debug("Updating binary sensor: %s", self.valve.identifier)
self.async_write_ha_state()
10 changes: 5 additions & 5 deletions custom_components/alpha_innotec/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ async def async_setup_entry(hass, entry, async_add_entities):
controller_api = hass.data[DOMAIN][entry.entry_id]['controller_api']
gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api']

coordinator = AlphaCoordinator(hass, controller_api, gateway_api)
coordinator = AlphaInnotecClimateCoordinator(hass, controller_api, gateway_api)

await coordinator.async_config_entry_first_refresh()

entities = []

for thermostat in coordinator.data:
entities.append(AlphaHomeSensor(
entities.append(AlphaInnotecClimateSensor(
coordinator=coordinator,
api=controller_api,
name=thermostat.name,
Expand All @@ -49,7 +49,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
async_add_entities(entities)


class AlphaCoordinator(DataUpdateCoordinator, BaseCoordinator):
class AlphaInnotecClimateCoordinator(DataUpdateCoordinator, BaseCoordinator):
"""My custom coordinator."""

def __init__(self, hass: HomeAssistant, controller_api: ControllerAPI, gateway_api: GatewayAPI) -> None:
Expand All @@ -73,7 +73,7 @@ async def _async_update_data(self) -> list[Thermostat]:
return await self.get_thermostats(self.hass, self.gateway_api, self.controller_api)


class AlphaHomeSensor(CoordinatorEntity, ClimateEntity):
class AlphaInnotecClimateSensor(CoordinatorEntity, ClimateEntity):
"""Representation of a Sensor."""

_attr_precision = 0.1
Expand All @@ -82,7 +82,7 @@ class AlphaHomeSensor(CoordinatorEntity, ClimateEntity):
ClimateEntityFeature.TARGET_TEMPERATURE
)

def __init__(self, coordinator: AlphaCoordinator, api: ControllerAPI, name: str, description: ClimateEntityDescription, thermostat: Thermostat) -> None:
def __init__(self, coordinator: AlphaInnotecClimateCoordinator, api: ControllerAPI, name: str, description: ClimateEntityDescription, thermostat: Thermostat) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator, context=thermostat.identifier)
self.api = api
Expand Down
24 changes: 18 additions & 6 deletions custom_components/alpha_innotec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import timedelta

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorDeviceClass
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand All @@ -26,7 +26,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
controller_api = hass.data[DOMAIN][entry.entry_id]['controller_api']
gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api']

coordinator = AlphaCoordinator(hass, controller_api, gateway_api)
coordinator = AlphaInnotecSensorCoordinator(hass, controller_api, gateway_api)

await coordinator.async_config_entry_first_refresh()

Expand All @@ -37,7 +37,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
_LOGGER.warning("Skipping %s because battery status is unknown.", thermostat.name)
continue

entities.append(AlphaHomeBatterySensor(
entities.append(AlphaInnotecBatterySensor(
coordinator=coordinator,
name=thermostat.name,
description=SensorEntityDescription(""),
Expand All @@ -47,7 +47,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
async_add_entities(entities)


class AlphaCoordinator(DataUpdateCoordinator, BaseCoordinator):
class AlphaInnotecSensorCoordinator(DataUpdateCoordinator, BaseCoordinator):
"""My custom coordinator."""

def __init__(self, hass: HomeAssistant, controller_api: ControllerAPI, gateway_api: GatewayAPI):
Expand All @@ -71,13 +71,13 @@ async def _async_update_data(self) -> list[Thermostat]:
return await self.get_thermostats(self.hass, self.gateway_api, self.controller_api)


class AlphaHomeBatterySensor(CoordinatorEntity, SensorEntity):
class AlphaInnotecBatterySensor(CoordinatorEntity, SensorEntity):
"""Representation of a Sensor."""

_attr_device_class = SensorDeviceClass.BATTERY
_attr_native_unit_of_measurement = "%"

def __init__(self, coordinator: AlphaCoordinator, name: str, description: SensorEntityDescription, thermostat: Thermostat) -> None:
def __init__(self, coordinator: AlphaInnotecSensorCoordinator, name: str, description: SensorEntityDescription, thermostat: Thermostat) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator, context=thermostat.identifier)
self.entity_description = description
Expand All @@ -100,3 +100,15 @@ def device_info(self) -> DeviceInfo:
def unique_id(self) -> str:
"""Return unique ID for this device."""
return self.thermostat.identifier

@callback
def _handle_coordinator_update(self) -> None:
for thermostat in self.coordinator.data:
if thermostat.identifier == self.thermostat.identifier:
self.thermostat = thermostat
break

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

self._attr_native_value = self.thermostat.battery_percentage
self.async_write_ha_state()

1 comment on commit a1ea6ff

@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
base_coordinator.py36360%2, 4, 6, 8–11, 13, 16, 18–21, 23, 25–26, 28–31, 33–34, 36–38, 40, 42–44, 46, 58–60, 62–64
binary_sensor.py67670%2, 4–5, 7, 9–12, 17–19, 21, 24, 27, 29, 31, 33, 35, 37–38, 45, 48, 51, 53, 60, 62, 65, 67, 69–70, 72–73, 75–76, 85, 87, 89, 92, 95, 97–101, 103–104, 106, 114–116, 118–119, 121, 123–125, 127–129, 131–136, 138–139
climate.py81810%2, 4–5, 7, 9, 13–15, 20–23, 25, 28, 31–32, 34, 36, 38, 40–41, 49, 52, 55, 57, 64–65, 67, 73, 76, 79–81, 85, 87–93, 95–96, 98, 106–107, 109, 111–112, 115, 117–119, 121–122, 124–126, 128–129, 131, 133, 136–137, 139–141, 143, 145–146, 148, 150, 152–154, 156–157, 159, 161–162, 165
config_flow.py371559%24–25, 27–30, 32–35, 57–61
const.py70100% 
controller_api.py765922%17–18, 20–21, 23–26, 28, 30–33, 35, 38, 40, 43, 45, 50, 52–55, 57, 59–60, 62, 64, 71, 73, 80–81, 83, 85, 87, 89, 91–92, 94, 97, 100–101, 103–106, 108, 111, 114, 120, 122–126, 138–140, 142
gateway_api.py624724%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, 81–82, 84, 87–88, 90, 93, 96–98, 100
sensor.py55550%2, 4–5, 7–10, 15–18, 20, 23, 26–27, 29, 31, 33, 35–38, 40, 47, 50, 53, 55, 62–63, 65, 71, 74, 77–78, 80, 82–86, 88–89, 91, 99–100, 102, 104–109, 111, 113–114
structs
   Thermostat.py11918%8–16
TOTAL49738023% 

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

Please sign in to comment.