Skip to content

Commit

Permalink
refactor: remove static SENSOR_MIXER_KEY and generate it dynamically …
Browse files Browse the repository at this point in the history
…based on available mixers
  • Loading branch information
jontofront committed Dec 5, 2024
1 parent 0981167 commit 775d449
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
22 changes: 0 additions & 22 deletions custom_components/econet300/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,6 @@
}
}

SENSOR_MIXER_KEY_TEST = {
"mixerTemp1",
"mixerTemp2",
"mixerTemp3",
"mixerTemp4",
"mixerSetTemp1",
"mixerSetTemp2",
"mixerSetTemp3",
}

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


NUMBER_MAP = {
"1280": "tempCOSet",
"1281": "tempCWUSet",
Expand Down
16 changes: 10 additions & 6 deletions custom_components/econet300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
ENTITY_UNIT_MAP,
ENTITY_VALUE_PROCESSOR,
SENSOR_MAP_KEY,
SENSOR_MIXER_KEY,
SERVICE_API,
SERVICE_COORDINATOR,
STATE_CLASS_MAP,
Expand Down Expand Up @@ -149,6 +148,13 @@ def create_mixer_sensor_entity_description(key: str) -> EconetSensorEntityDescri
return entity_description


# Dynamically generate SENSOR_MIXER_KEY
SENSOR_MIXER_KEY = {
str(i): {f"mixerTemp{i}", f"mixerSetTemp{i}"}
for i in range(1, AVAILABLE_NUMBER_OF_MIXERS + 1)
}


def create_mixer_sensors(
coordinator: EconetDataCoordinator, api: Econet300Api
) -> list[MixerSensor]:
Expand All @@ -161,24 +167,22 @@ def create_mixer_sensors(

if not mixer_keys:
_LOGGER.debug(
"Maišytuvas: %s nėra apibrėžtas konstantoje, nebus pridėtas.", i
"Mixer: %s is not defined in the constants and will not be added.", 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
)
_LOGGER.warning("Mixer: %s will not be created due to invalid data.", 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)
_LOGGER.debug("Added Mixer: %s, Sensor: %s", i, key)

return entities

Expand Down

0 comments on commit 775d449

Please sign in to comment.