Skip to content

Commit

Permalink
Merge pull request #34 from radical-squared/fixes-for-v3-0-4
Browse files Browse the repository at this point in the history
Fixes for v3.0.4
  • Loading branch information
elad-bar authored Jun 4, 2023
2 parents 41841f8 + d54ee22 commit 602f3c8
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 263 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v3.0.4

- Add device discovery for new devices without need to reload integration
- Fix integration reload issue
- Entities code clean up
- API stabilization after timeout
- Fix target temperature parameter mapping (Heat, Cool and Auto)
- Fix parameter mapping for P01 (Water pump mode)

## v3.0.3

- Fix setting HVAC mode
Expand Down
39 changes: 25 additions & 14 deletions custom_components/aqua_temp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
initialized = False

try:
platforms = _get_platforms()

config_manager = AquaTempConfigManager(hass, entry)
await config_manager.initialize()

Expand All @@ -32,23 +34,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

await coordinator.async_config_entry_first_refresh()

_LOGGER.info("Finished loading integration")

platforms = []
for entity_description in ALL_ENTITIES:
if (
entity_description.platform not in platforms
and entity_description.platform is not None
):
platforms.append(entity_description.platform)
await hass.config_entries.async_forward_entry_setups(entry, platforms)

_LOGGER.debug(f"Loading platforms: {platforms}")
_LOGGER.info(f"Start loading {DOMAIN} integration, Entry ID: {entry.entry_id}")

await hass.config_entries.async_forward_entry_setups(entry, platforms)
await coordinator.async_config_entry_first_refresh()

_LOGGER.info("Finished loading components")
_LOGGER.info("Finished loading integration")

initialized = True

Expand All @@ -66,6 +58,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
_LOGGER.info(f"Unloading {DOMAIN} integration, Entry ID: {entry.entry_id}")

platforms = _get_platforms()

for platform in platforms:
await hass.config_entries.async_forward_entry_unload(entry, platform)

del hass.data[DOMAIN][entry.entry_id]

return True


def _get_platforms():
platforms = []
for entity_description in ALL_ENTITIES:
if (
entity_description.platform not in platforms
and entity_description.platform is not None
):
platforms.append(entity_description.platform)

return platforms
83 changes: 47 additions & 36 deletions custom_components/aqua_temp/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import logging
import sys

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify

from .common.consts import ALL_ENTITIES, DOMAIN
from .common.consts import DOMAIN, SIGNAL_AQUA_TEMP_DEVICE_NEW
from .common.device_discovery import (
async_handle_discovered_device,
find_entity_description,
)
from .common.entity_descriptions import AquaTempBinarySensorEntityDescription
from .managers.aqua_temp_coordinator import AquaTempCoordinator

Expand All @@ -18,33 +22,35 @@
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
):
"""Set up the sensor platform."""
try:
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = []
entity_descriptions = []

for entity_description in ALL_ENTITIES:
if entity_description.platform == Platform.BINARY_SENSOR:
entity_descriptions.append(entity_description)

for device_code in coordinator.api_data:
for entity_description in entity_descriptions:
entity = AquaTempBinarySensorEntity(
device_code, entity_description, coordinator
)
def _create(
device_code: str, entity_description_key: str, coordinator: AquaTempCoordinator
) -> AquaTempBinarySensorEntity:
entity_description = find_entity_description(
entity_description_key, Platform.BINARY_SENSOR
)

entities.append(entity)
entity = AquaTempBinarySensorEntity(
device_code, entity_description, coordinator
)

_LOGGER.debug(f"Setting up binary sensor entities: {entities}")
return entity

async_add_entities(entities, True)
@callback
def _async_device_new(device_code):
coordinator = hass.data[DOMAIN][entry.entry_id]

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
async_handle_discovered_device(
device_code,
coordinator,
Platform.BINARY_SENSOR,
_create,
async_add_entities,
)

_LOGGER.error(f"Failed to initialize select, Error: {ex}, Line: {line_number}")
"""Set up the binary sensor platform."""
entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_AQUA_TEMP_DEVICE_NEW, _async_device_new)
)


class AquaTempBinarySensorEntity(CoordinatorEntity, BinarySensorEntity):
Expand All @@ -59,40 +65,45 @@ def __init__(
super().__init__(coordinator)

self._device_code = device_code
self._api_data = self.coordinator.api_data[self._device_code]
self._config_data = self.coordinator.config_data
self._config_data = coordinator.config_data

device_info = coordinator.get_device(device_code)
device_name = device_info.get("name")
device_data = coordinator.get_device_data(device_code)

entity_name = f"{device_name} {entity_description.name}"

slugify_name = slugify(entity_name)

device_id = self._api_data.get("device_id")
device_id = device_data.get("device_id")
unique_id = slugify(f"{entity_description.platform}_{slugify_name}_{device_id}")

self.entity_description: AquaTempBinarySensorEntityDescription = (
entity_description
)
self.entity_description = entity_description

self._attr_device_info = device_info
self._attr_name = entity_name
self._attr_unique_id = unique_id
self._attr_device_class = entity_description.device_class
self._entity_attributes = entity_description.attributes
self._entity_on_value = entity_description.on_value

@property
def _local_coordinator(self) -> AquaTempCoordinator:
return self.coordinator

def _handle_coordinator_update(self) -> None:
"""Fetch new state data for the sensor."""
status = self._api_data.get(self.entity_description.key)
device_data = self._local_coordinator.get_device_data(self._device_code)
status = device_data.get(self.entity_description.key)

attributes = {}
if self.entity_description.attributes is not None:
for attribute_key in self.entity_description.attributes:
value = self._api_data.get(attribute_key)
if self._entity_attributes is not None:
for attribute_key in self._entity_attributes:
value = device_data.get(attribute_key)

attributes[attribute_key] = value

self._attr_is_on = status == self.entity_description.on_value
self._attr_is_on = status == self._entity_on_value
self._attr_extra_state_attributes = attributes

self.async_write_ha_state()
Loading

0 comments on commit 602f3c8

Please sign in to comment.