Skip to content

Commit

Permalink
Keep data in api
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Jan 15, 2025
1 parent 3e0d42a commit 1503a22
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 318 deletions.
4 changes: 2 additions & 2 deletions custom_components/dabpumps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create(hass: HomeAssistant, username, password):
client = async_create_clientsession(hass)

# Create a new DabPumpsApi instance
api = DabPumpsApi(hass, username, password, client=client)
api = DabPumpsApi(username, password, client=client)

# Remember this DabPumpsApi instance
hass.data[DOMAIN][API][key] = api
Expand All @@ -60,7 +60,7 @@ def create_temp(hass: HomeAssistant, username, password):
client = async_create_clientsession(hass)

# Create a new DabPumpsApi instance
api = DabPumpsApi(hass, username, password, client=client)
api = DabPumpsApi(username, password, client=client)

return api

Expand Down
94 changes: 51 additions & 43 deletions custom_components/dabpumps/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
from collections import defaultdict
from collections import namedtuple

from aiodabpumps import (
DabPumpsDevice,
DabPumpsParams,
DabPumpsStatus
)

from .const import (
DOMAIN,
Expand All @@ -45,6 +50,10 @@
BINARY_SENSOR_VALUES_ALL,
)

from .coordinator import (
DabPumpsCoordinator,
)

from .entity_base import (
DabPumpsEntityHelperFactory,
DabPumpsEntityHelper,
Expand Down Expand Up @@ -77,22 +86,47 @@ class DabPumpsBinarySensor(CoordinatorEntity, BinarySensorEntity, DabPumpsEntity
Could be a sensor that is part of a pump like ESybox, Esybox.mini
Or could be part of a communication module like DConnect Box/Box2
"""
def __init__(self, coordinator, install_id, object_id, device, params, status) -> None:
""" Initialize the sensor. """
def __init__(self, coordinator: DabPumpsCoordinator, install_id: str, object_id: str, unique_id: str, device: DabPumpsDevice, params: DabPumpsParams, status: DabPumpsStatus) -> None:
"""
Initialize the sensor.
"""

CoordinatorEntity.__init__(self, coordinator)
DabPumpsEntity.__init__(self, coordinator, params)

# The unique identifier for this sensor within Home Assistant
self.object_id = object_id
self.entity_id = ENTITY_ID_FORMAT.format(status.unique_id)
# Sanity check
if params.type != 'enum':
_LOGGER.error(f"Unexpected parameter type ({self._params.type}) for a binary sensor")

if len(params.values or []) != 2:
_LOGGER.error(f"Unexpected parameter values ({self._params.values}) for a binary sensor")

# The unique identifiers for this sensor within Home Assistant
self.object_id = object_id # Device.serial + status.key
self.entity_id = ENTITY_ID_FORMAT.format(unique_id) # Device.name + status.key
self.install_id = install_id

self._coordinator = coordinator
self._device = device
self._params = params

# Create all attributes
self._update_attributes(status, True)
# update creation-time only attributes
_LOGGER.debug(f"Create entity '{self.entity_id}'")

self._attr_unique_id = unique_id

self._attr_has_entity_name = True
self._attr_name = self._get_string(status.key)
self._name = status.key

self._attr_device_class = self._get_device_class()

self._attr_device_info = DeviceInfo(
identifiers = {(DOMAIN, self._device.serial)},
)

# Create all value related attributes
self._update_attributes(status, force=True)


@property
Expand Down Expand Up @@ -120,22 +154,16 @@ def _handle_coordinator_update(self) -> None:
# find the correct device and status corresponding to this sensor
(_, _, status_map) = self._coordinator.data
status = status_map.get(self.object_id)
if not status:
return

# Update any attributes
if status:
if self._update_attributes(status, False):
self.async_write_ha_state()
if self._update_attributes(status):
self.async_write_ha_state()


def _update_attributes(self, status, is_create):
def _update_attributes(self, status: DabPumpsStatus, force: bool = False):

# Sanity check
if self._params.type != 'enum':
_LOGGER.error(f"Unexpected parameter type ({self._params.type}) for a binary sensor")

if len(self._params.values or []) != 2:
_LOGGER.error(f"Unexpected parameter values ({self._params.values}) for a binary sensor")

# Lookup the dict string for the value and otherwise return the value itself
val = self._params.values.get(status.val, status.val)
if val in BINARY_SENSOR_VALUES_ON:
Expand All @@ -145,34 +173,14 @@ def _update_attributes(self, status, is_create):
else:
is_on = None

# Process any changes
changed = False

# update creation-time only attributes
if is_create:
_LOGGER.debug(f"Create binary_sensor '{status.key}' ({status.unique_id})")

self._attr_unique_id = status.unique_id

self._attr_has_entity_name = True
self._attr_name = self._get_string(status.key)
self._name = status.key

self._attr_device_class = self._get_device_class()

self._attr_device_info = DeviceInfo(
identifiers = {(DOMAIN, self._device.serial)},
)
changed = True

# update value if it has changed
if is_create \
or (self._attr_is_on != is_on):
if (self._attr_is_on != is_on) or force:

self._attr_is_on = is_on
changed = True

return changed
return True

# No changes
return False


def _get_device_class(self):
Expand Down
6 changes: 0 additions & 6 deletions custom_components/dabpumps/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@
# "": "Thai"
}

# Extra device attributes that are not in install info, but retrieved from statusses
DEVICE_ATTR_EXTRA = {
"mac_address": ['MacWlan'],
"sw_version": ['LvFwVersion', 'ucVersion']
}

LANGUAGE_TEXT_AUTO ="Auto (use system setting: {0})"
LANGUAGE_TEXT_FALLBACK ="Auto (use default: {0})"

Expand Down
Loading

0 comments on commit 1503a22

Please sign in to comment.