Skip to content

Commit

Permalink
fix:sensor update by removing coordinator (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeLe authored Dec 31, 2024
1 parent fe32938 commit 304015c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 191 deletions.
138 changes: 0 additions & 138 deletions custom_components/aiseg2/coordinator.py

This file was deleted.

2 changes: 1 addition & 1 deletion custom_components/aiseg2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"requirements": ["lxml"],
"ssdp": [],
"zeroconf": [],
"version": "0.0.1"
"version": "0.1.0"
}
63 changes: 33 additions & 30 deletions custom_components/aiseg2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

from __future__ import annotations

from datetime import datetime
from datetime import datetime, timedelta

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.dt import async_get_time_zone

from . import AisegConfigEntry
from .aiseg_api import AisegEntityType
from .coordinator import AisegPoolingCoordinator
from .aiseg_api import AisegEnergySensor, AisegEntityType, AisegPowerSensor
from .const import DOMAIN

SCAN_INTERVAL = timedelta(seconds=30)


async def async_setup_entry(
Expand All @@ -27,7 +28,6 @@ async def async_setup_entry(
) -> None:
"""Config entry example."""
my_api = entry.runtime_data
coordinator = AisegPoolingCoordinator(hass, my_api)

# Fetch initial data so we have data when entities subscribe
#
Expand All @@ -38,21 +38,30 @@ async def async_setup_entry(
# coordinator.async_refresh() instead
#
tz = await async_get_time_zone(hass.config.time_zone)
await coordinator.async_config_entry_first_refresh()
device_info = coordinator.getDeviceInfo()
data = await my_api.fetch_data()
device = await my_api.get_device()
if device is not None:
device_info = {
"name": device.name,
"identifiers": {(DOMAIN, device.device_id)},
"manufacturer": device.manufacturer,
}
else:
device_info = {}

energy_entities = [
EnergySensor(coordinator, item.getKey(), item.getValue(), device_info, tz)
for item in coordinator.data.getByType(AisegEntityType.ENERGY)
EnergySensor(item, item.getKey(), item.getValue(), device_info, tz)
for item in filter(lambda datum: datum.type == AisegEntityType.ENERGY, data)
]
power_entities = [
PowerSensor(coordinator, item.getKey(), item.getValue(), device_info)
for item in coordinator.data.getByType(AisegEntityType.POWER)
PowerSensor(item, item.getKey(), item.getValue(), device_info)
for item in filter(lambda datum: datum.type == AisegEntityType.POWER, data)
]
async_add_entities(energy_entities)
async_add_entities(power_entities)


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

_attr_name = "Power"
Expand All @@ -62,11 +71,11 @@ class PowerSensor(CoordinatorEntity, SensorEntity):
_attr_native_value = 0

def __init__(
self, coordinator: AisegPoolingCoordinator, idx, initial_value, device_info
self, sensor: AisegPowerSensor, idx, initial_value, device_info
) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator, context=idx)
self.idx = idx
self.sensor = sensor
self._attr_unique_id = idx
self._attr_device_info = device_info
self._attr_name = idx
Expand All @@ -76,15 +85,12 @@ def __init__(
def translation_key(self):
return self._attr_name

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
item = self.coordinator.data.get(self.idx)
self._attr_native_value = item.getValue()
self.async_write_ha_state()
async def async_update(self) -> None:
"""Update sensor value."""
self._attr_native_value = await self.sensor.update()


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

_attr_name = "Energy"
Expand All @@ -101,12 +107,12 @@ def _get_today_start_time(self):
)

def __init__(
self, coordinator: AisegPoolingCoordinator, idx, initial_value, device_info, tz
self, sensor: AisegEnergySensor, idx, initial_value, device_info, tz
) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator, context=idx)
self.tz = tz
self.idx = idx
self.sensor = sensor
self._attr_unique_id = idx
self._attr_device_info = device_info
self._attr_name = idx
Expand All @@ -117,10 +123,7 @@ def __init__(
def translation_key(self):
return self._attr_name

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
item = self.coordinator.data.get(self.idx)
self._attr_native_value = item.getValue()
async def async_update(self) -> None:
"""Update sensor value."""
self._attr_native_value = await self.sensor.update()
self._attr_last_reset = self._get_today_start_time()
self.async_write_ha_state()
48 changes: 26 additions & 22 deletions custom_components/aiseg2/switch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Platform for switch integration."""

from datetime import timedelta

from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import AisegConfigEntry
from .aiseg_api import AisegEntityType
from .coordinator import AisegPoolingCoordinator
from .aiseg_api import AisegEntityType, AisegSwitch
from .const import DOMAIN

SCAN_INTERVAL = timedelta(seconds=10)


async def async_setup_entry(
Expand All @@ -17,7 +20,6 @@ async def async_setup_entry(
) -> None:
"""Config entry example."""
my_api = entry.runtime_data
coordinator = AisegPoolingCoordinator(hass, my_api, update_interval=10)

# Fetch initial data so we have data when entities subscribe
#
Expand All @@ -27,39 +29,41 @@ async def async_setup_entry(
# If you do not want to retry setup on failure, use
# coordinator.async_refresh() instead
#
await coordinator.async_config_entry_first_refresh()
device_info = coordinator.getDeviceInfo()
data = await my_api.fetch_data()
device = await my_api.get_device()
if device is not None:
device_info = {
"name": device.name,
"identifiers": {(DOMAIN, device.device_id)},
"manufacturer": device.manufacturer,
}
else:
device_info = {}

switch_entities = [
NotificationEnableSwitch(
coordinator, item.getKey(), item.getValue(), device_info
)
for item in coordinator.data.getByType(AisegEntityType.SWITCH)
NotificationEnableSwitch(item, item.getKey(), item.getValue(), device_info)
for item in filter(lambda datum: datum.type == AisegEntityType.SWITCH, data)
]

async_add_entities(switch_entities)


class NotificationEnableSwitch(CoordinatorEntity, SwitchEntity):
class NotificationEnableSwitch(SwitchEntity):
"""Entity to manipulate AiSEG config switch."""

__attr_name = "notification_enabled"
__attr_device_class = SwitchDeviceClass.SWITCH

def __init__(
self, coordinator: AisegPoolingCoordinator, idx, initial_value, device_info
) -> None:
def __init__(self, switch: AisegSwitch, idx, initial_value, device_info) -> None:
"""Initialize."""
super().__init__(coordinator, context=idx)
self.idx = idx
self.switch = switch
self._attr_unique_id = idx
self._attr_name = idx
self.is_on = initial_value
self._attr_device_info = device_info
self.is_on = False

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
item = self.coordinator.data.get(self.idx)
self.is_on = item.getValue()
self.async_write_ha_state()
async def async_update(self):
"""Update switch state."""
self.is_on = self.switch.getValue()

0 comments on commit 304015c

Please sign in to comment.