From f2cc9f5024af6b488545a804459bb278bd83ea23 Mon Sep 17 00:00:00 2001 From: RubenKelevra Date: Thu, 19 Dec 2024 22:22:44 +0100 Subject: [PATCH] add voltage meter user query --- utils/measure/.env.dist | 1 + utils/measure/measure/powermeter/const.py | 1 + utils/measure/measure/powermeter/hass.py | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/utils/measure/.env.dist b/utils/measure/.env.dist index d0acf2bfa..268f87664 100644 --- a/utils/measure/.env.dist +++ b/utils/measure/.env.dist @@ -93,4 +93,5 @@ MYSTROM_DEVICE_IP=x.x.x.x # MODEL_NAME=xx # DUMMY_LOAD=true # POWERMETER_ENTITY_ID=sensor.my_power +# VOLTAGEMETER_ENTITY_ID=sensor.my_voltage # RESUME=true diff --git a/utils/measure/measure/powermeter/const.py b/utils/measure/measure/powermeter/const.py index ad8e162d0..f0e4a3365 100644 --- a/utils/measure/measure/powermeter/const.py +++ b/utils/measure/measure/powermeter/const.py @@ -14,6 +14,7 @@ class PowerMeterType(str, Enum): QUESTION_POWERMETER_ENTITY_ID = "powermeter_entity_id" +QUESTION_VOLTAGEMETER_ENTITY_ID = "voltagemeter_entity_id" class Trend(StrEnum): diff --git a/utils/measure/measure/powermeter/hass.py b/utils/measure/measure/powermeter/hass.py index 3878f21fe..21f0e2d16 100644 --- a/utils/measure/measure/powermeter/hass.py +++ b/utils/measure/measure/powermeter/hass.py @@ -6,7 +6,7 @@ import inquirer from homeassistant_api import Client -from measure.powermeter.const import QUESTION_POWERMETER_ENTITY_ID +from measure.powermeter.const import QUESTION_POWERMETER_ENTITY_ID, QUESTION_VOLTAGEMETER_ENTITY_ID from measure.powermeter.errors import PowerMeterError, UnsupportedFeatureError from measure.powermeter.powermeter import PowerMeasurementResult, PowerMeter @@ -66,8 +66,22 @@ def has_voltage_support(self) -> bool: def find_voltage_entity(self) -> str | None: """Try to find a matching voltage entity for the current power entity.""" matched_sensors = self.match_power_and_voltage_sensors() + + if not matched_sensors: + return self.get_voltage_question(self.get_voltage_sensors()) + return matched_sensors.get(self._entity_id) + def get_voltage_question(self, voltage_sensor_list: list[str]) -> str | None: + """Return a question to select a voltage sensor.""" + return [ + inquirer.List( + name=QUESTION_VOLTAGEMETER_ENTITY_ID, + message="Select the voltage sensor", + choices=voltage_sensor_list, + ) + ] + def get_questions(self) -> list[inquirer.questions.Question]: power_sensor_list = self.get_power_sensors() @@ -108,3 +122,4 @@ def match_power_and_voltage_sensors(self) -> dict[str, str]: def process_answers(self, answers: dict[str, Any]) -> None: self._entity_id = answers[QUESTION_POWERMETER_ENTITY_ID] + self._voltage_entity_id = answers[QUESTION_VOLTAGEMETER_ENTITY_ID]