Skip to content

Commit

Permalink
refactor: reorganize mixer sensor keys add mixer only whet temp is no…
Browse files Browse the repository at this point in the history
…t NULL and enhance sensor creation logic
  • Loading branch information
jontofront committed Dec 5, 2024
1 parent 8c0554f commit 0981167
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
20 changes: 13 additions & 7 deletions custom_components/econet300/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@
},
}

SENSOR_MIXER_KEY = {
"1": {
"mixerTemp1",
"mixerSetTemp1",
}
}

BINARY_SENSOR_MAP_KEY = {
"_default": {
"lighter",
Expand Down Expand Up @@ -189,6 +182,19 @@
"mixerSetTemp2",
"mixerSetTemp3",
}

SENSOR_MIXER_KEY = {
"1": {
"mixerTemp1",
"mixerSetTemp1",
},
"2": {
"mixerTemp2",
"mixerSetTemp2",
},
}


NUMBER_MAP = {
"1280": "tempCOSet",
"1281": "tempCWUSet",
Expand Down
37 changes: 23 additions & 14 deletions custom_components/econet300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,36 @@ def create_mixer_sensor_entity_description(key: str) -> EconetSensorEntityDescri
return entity_description


def create_mixer_sensors(coordinator: EconetDataCoordinator, api: Econet300Api):
def create_mixer_sensors(
coordinator: EconetDataCoordinator, api: Econet300Api
) -> list[MixerSensor]:
"""Create individual sensor descriptions for mixer sensors."""
entities: list[MixerSensor] = []
# TODO: Cleanup logic add mixer only which not null in endpoint

for i in range(1, AVAILABLE_NUMBER_OF_MIXERS + 1):
string_mix = str(i)
mixer_keys = SENSOR_MIXER_KEY.get(string_mix)
if mixer_keys:
for key in mixer_keys:
if can_add_mixer(key, coordinator):
mixer_sensor_entity = create_mixer_sensor_entity_description(key)
entities.append(
MixerSensor(mixer_sensor_entity, coordinator, api, i)
)
else:
_LOGGER.warning("Mixer: %s , Sensor: %s won't be added", i, key)
else:

if not mixer_keys:
_LOGGER.debug(
"Mixer: %s not defined in const, won't be added",
i,
"Maišytuvas: %s nėra apibrėžtas konstantoje, nebus pridėtas.", i
)
continue

# Check if all required mixer keys have valid (non-null) values
if any(
coordinator.data.get("regParams", {}).get(key) is None for key in mixer_keys
):
_LOGGER.warning(
"Maišytuvas: %s nebus sukurtas dėl negaliojančių duomenų.", i
)
continue

# Create sensors for this mixer
for key in mixer_keys:
mixer_sensor_entity = create_mixer_sensor_entity_description(key)
entities.append(MixerSensor(mixer_sensor_entity, coordinator, api, i))
_LOGGER.debug("Pridėtas maišytuvas: %s, jutiklis: %s", i, key)

return entities

Expand Down

0 comments on commit 0981167

Please sign in to comment.