Skip to content

Commit

Permalink
Allow to configure entities from group Setpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed May 7, 2024
1 parent 0e611ab commit c8cf3f7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
11 changes: 1 addition & 10 deletions custom_components/dabpumps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .const import (
STARTUP_MESSAGE,
DOMAIN,
PLATFORMS,
API,
COORDINATOR,
HELPER,
Expand All @@ -38,16 +39,6 @@
_LOGGER.info(STARTUP_MESSAGE)


PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SELECT,
Platform.SWITCH,

]


CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)


Expand Down
11 changes: 10 additions & 1 deletion custom_components/dabpumps/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
CONF_USERNAME,
CONF_PASSWORD,
)
from homeassistant.const import Platform

_LOGGER: logging.Logger = logging.getLogger(__package__)
#_LOGGER = logging.getLogger("custom_components.dabpumps")

# Base component constants
DOMAIN = "dabpumps"
NAME = "DAB Pumps"
VERSION="2024.04.4"
VERSION="2024.05.1"
ISSUE_URL = "https://github.com/ankoh/dabpumps/issues"

PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SELECT,
Platform.SWITCH,
]

HUB = "Hub"
API = "Api"
COORDINATOR = "Coordinator"
Expand Down
34 changes: 28 additions & 6 deletions custom_components/dabpumps/entity_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .const import (
DOMAIN,
PLATFORMS,
NAME,
HELPER,
CONF_INSTALL_ID,
Expand Down Expand Up @@ -46,6 +47,8 @@
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback

import homeassistant.helpers.entity_registry as entity_registry


from .const import (
DOMAIN,
Expand Down Expand Up @@ -107,6 +110,9 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, install_id, i

# Get an instance of the DabPumpsCoordinator for this install_id
self.coordinator = DabPumpsCoordinatorFactory.create(hass, config_entry)

# Get entity registry
self.entity_registry = entity_registry.async_get(hass)


async def async_setup_entry(self, target_platform, target_class, async_add_entities: AddEntitiesCallback):
Expand All @@ -121,8 +127,10 @@ async def async_setup_entry(self, target_platform, target_class, async_add_entit
_LOGGER.warning(f"Failed to fetch sensor data - authentication failed or no data.")
return

_LOGGER.debug(f"Create entities for installation '{self.install_name}' ({self.install_id})")
other_platforms = [p for p in PLATFORMS if p != target_platform]

_LOGGER.debug(f"Create entities for installation '{self.install_name}' ({self.install_id})")

# Iterate all statusses to create sensor entities
entities = []
for object_id, status in status_map.items():
Expand Down Expand Up @@ -152,13 +160,25 @@ async def async_setup_entry(self, target_platform, target_class, async_add_entit
# This status will be handled via another platform
continue

# Create a Sensor, Binary_Sensor, Number, Select, Switch or other entity for this status
entity = None
try:
# Create a Sensor, Binary_Sensor, or other entity for this status
entity = target_class(self.coordinator, self.install_id, object_id, device, params, status)
entities.append(entity)
except Exception as ex:
_LOGGER.warning(f"Could not instantiate {platform} entity class for {object_id}. Details: {ex}")


# See if new entity already existed under another platform. If so, then remove the old entity.
if entity:
for p in other_platforms:
try:
entity_id = self.entity_registry.async_get_entity_id(p, DOMAIN, entity.unique_id)
if entity_id:
_LOGGER.info(f"Remove obsolete {entity_id} that is replaced by {platform}.{entity.unique_id}")
self.entity_registry.async_remove(entity_id)
except Exception as ex:
_LOGGER.warning(f"Could not remove obsolete {p}.{entity.unique_id} entity. Details: {ex}")

_LOGGER.info(f"Add {len(entities)} {target_platform} entities for installation '{self.install_name}' with {len(device_map)} devices")
if entities:
async_add_entities(entities)
Expand Down Expand Up @@ -223,10 +243,11 @@ def _get_entity_platform(self, params):
# And needs to be in group 'Extra Comfort' or be a specific key
# that would otherwise be excluded as group
keys_config = [
'PumpDisable'
'PumpDisable',
]
groups_config = [
'Extra Comfort'
'Extra Comfort',
'Setpoint',
]
is_config = False
if self.coordinator.user_role in params.change:
Expand All @@ -246,7 +267,7 @@ def _get_entity_platform(self, params):
return Platform.SELECT

# Is it a numeric type?
elif params.type == 'measure' and params.min is not None and params.max is not None:
elif params.type == 'measure':
return Platform.NUMBER

# Is it a binary sensor?
Expand Down Expand Up @@ -499,6 +520,7 @@ def get_entity_category(self):
# Typically intended for restart or update functionality
groups_config = [
'System Management',
'Setpoint'
]
if self._params.group in groups_config and 'I' in self._params.change:
return EntityCategory.CONFIG
Expand Down
2 changes: 1 addition & 1 deletion custom_components/dabpumps/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"issue_tracker": "https://github.com/ankohanse/hass-dab-pumps/issues",
"loggers": ["custom_components.dabpumps"],
"requirements": [],
"version": "2024.04.4"
"version": "2024.05.1"
}
12 changes: 6 additions & 6 deletions custom_components/dabpumps/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ def _update_attributes(self, status, is_create):
if self._params.weight and self._params.weight != 1 and self._params.weight != 0:
# Convert to float
attr_precision = int(math.floor(math.log10(1.0 / self._params.weight)))
attr_min = round(float(self._params.min) * self._params.weight, attr_precision)
attr_max = round(float(self._params.max) * self._params.weight, attr_precision)
attr_val = round(float(status.val) * self._params.weight, attr_precision) if status.val!=None else None
attr_min = round(float(self._params.min) * self._params.weight, attr_precision) if self._params.min is not None else None
attr_max = round(float(self._params.max) * self._params.weight, attr_precision) if self._params.max is not None else None
attr_val = round(float(status.val) * self._params.weight, attr_precision) if status.val is not None else None
attr_step = self._params.weight
else:
# Convert to int
attr_precision = 0
attr_min = int(self._params.min)
attr_max = int(self._params.max)
attr_val = int(status.val) if status.val!=None else None
attr_min = int(self._params.min) if self._params.min is not None else None
attr_max = int(self._params.max) if self._params.max is not None else None
attr_val = int(status.val) if status.val is not None else None
attr_step = self.get_number_step()

# update creation-time only attributes
Expand Down

0 comments on commit c8cf3f7

Please sign in to comment.