Skip to content

Commit

Permalink
Rewriting mixer addition
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilKurkianec committed Feb 1, 2024
1 parent 87ddfbe commit 14ef3a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 48 deletions.
10 changes: 7 additions & 3 deletions custom_components/econet300/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
AVAILABLE_NUMBER_OF_MIXERS = 6
MIXER_AVAILABILITY_KEY = "mixerTemp"
MIXER_KEY = "mixerPumpWorks"
MIXER_SET_TEMP = "mixerSetTemp"

#######################
######## REG PARAM MAPS
Expand All @@ -112,15 +111,20 @@
"1028": "tempUpperBuffer",
"1029": "tempLowerBuffer",
"1030": "tempFlueGas",
"1031": "mixerTemp1",
"1287": "mixerSetTemp1",
"1792": "mode",
"1794": "boilerPower",
"1795": "fanPower",
"1280": "tempCOSet",
"1281": "tempCWUSet",
}

MIXER_MAP = {
"1": {
"1031": "mixerTemp1",
"1287": "mixerSetTemp1",
}
}

NUMBER_MAP = {
"1280": "tempCOSet",
"1281": "tempCWUSet",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/econet300/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(
def device_info(self) -> DeviceInfo | None:
"""Return device info of the entity."""
return DeviceInfo(
identifiers={(DOMAIN, f"{self.api.uid}-mixer-device-{self._idx}")},
identifiers={(DOMAIN, f"{self.api.uid}-mixer-{self._idx}")},
name=f"{DEVICE_INFO_MIXER_NAME}{self._idx}",
manufacturer=DEVICE_INFO_MANUFACTURER,
model=DEVICE_INFO_MODEL,
Expand Down
72 changes: 28 additions & 44 deletions custom_components/econet300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
ENTITY_PRECISION,
ENTITY_UNIT_MAP,
ENTITY_VALUE_PROCESSOR,
MIXER_AVAILABILITY_KEY,
MIXER_SET_TEMP,
SENSOR_MAP,
SERVICE_API,
SERVICE_COORDINATOR,
STATE_CLASS_MAP,
MIXER_MAP,
)
from .entity import EconetEntity, MixerEntity
from .common_functions import get_key_by_value

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -135,21 +133,23 @@ def can_add_mixer(key: str, coordinator: EconetDataCoordinator):


def create_mixer_sensor_entity_description(
key: str, entity_type: str
key: str, map_key: str
) -> EconetSensorEntityDescription:
"""Create Econect300 mixer sensor entity based on supplied key."""
_LOGGER.debug(
"Creating Mixer entity description for key: %s, and type : %s", key, entity_type
"Creating Mixer entity sensor description for key: %s, and type : %s",
key,
map_key,
)
entity_description = EconetSensorEntityDescription(
key=key,
translation_key=camel_to_snake(f"{entity_type}{key}"),
icon=ENTITY_ICON.get(entity_type, None),
native_unit_of_measurement=ENTITY_UNIT_MAP.get(entity_type, None),
state_class=STATE_CLASS_MAP.get(entity_type, None),
device_class=ENTITY_DEVICE_CLASS_MAP.get(entity_type, None),
suggested_display_precision=ENTITY_PRECISION.get(entity_type, 0),
process_val=ENTITY_VALUE_PROCESSOR.get(entity_type, lambda x: x),
translation_key=camel_to_snake(map_key),
icon=ENTITY_ICON.get(map_key, None),
native_unit_of_measurement=ENTITY_UNIT_MAP.get(map_key, None),
state_class=STATE_CLASS_MAP.get(map_key, None),
device_class=ENTITY_DEVICE_CLASS_MAP.get(map_key, None),
suggested_display_precision=ENTITY_PRECISION.get(map_key, 0),
process_val=ENTITY_VALUE_PROCESSOR.get(map_key, lambda x: x),
)
_LOGGER.debug("Created Mixer entity description: %s", entity_description)
return entity_description
Expand All @@ -160,41 +160,25 @@ def create_mixer_sensors(coordinator: EconetDataCoordinator, api: Econet300Api):
entities: list[MixerSensor] = []

for i in range(1, AVAILABLE_NUMBER_OF_MIXERS + 1):
mixer_temp_key = f"{MIXER_AVAILABILITY_KEY}{i}"
mixer_temp_id = get_key_by_value(SENSOR_MAP, mixer_temp_key)
if mixer_temp_id is None:
_LOGGER.warning(
"Mixer: %s not found, wont map",
mixer_temp_key,
)
elif can_add_mixer(mixer_temp_id, coordinator):
mixer_temp_entity = create_mixer_sensor_entity_description(
mixer_temp_id, MIXER_AVAILABILITY_KEY
)
entities.append(MixerSensor(mixer_temp_entity, coordinator, api, i))
if str(i) in MIXER_MAP:
for key, value in MIXER_MAP[i]:
if can_add_mixer(key, coordinator):
mixer_sensor_entity = create_mixer_sensor_entity_description(
key, value
)
entities.append(
MixerSensor(mixer_sensor_entity, coordinator, api, i)
)
else:
_LOGGER.warning(
"Mixer: %s , Sensor: %s %s wont be added", i, key, value
)
else:
_LOGGER.warning(
"Availability Mixer temperature key: %s does not exist, entity will not be added",
mixer_temp_key,
_LOGGER.debug(
"Mixer: %s not defined in const, wont be added",
i,
)

mixer_set_temp_key = f"{MIXER_SET_TEMP}{i}"
mixer_set_temp_id = get_key_by_value(SENSOR_MAP, mixer_set_temp_key)
if mixer_set_temp_id is None:
_LOGGER.warning(
"Mixer: %s not found, wont map",
mixer_temp_key,
)
elif can_add_mixer(mixer_set_temp_id, coordinator):
mixer_set_temp_entity = create_mixer_sensor_entity_description(
mixer_set_temp_id, MIXER_SET_TEMP
)
entities.append(MixerSensor(mixer_set_temp_entity, coordinator, api, i))
else:
_LOGGER.warning(
"Availability Mixer set temperature key: %s does not exist, entity will not be added",
mixer_set_temp_key,
)
return entities


Expand Down

0 comments on commit 14ef3a3

Please sign in to comment.