From 438c4143419105de3ff03706cdb1c934b6b46172 Mon Sep 17 00:00:00 2001 From: Hamish Findlay Date: Sat, 9 Sep 2023 21:12:57 +0100 Subject: [PATCH] Bug fixes --- custom_components/battery_sim/__init__.py | 34 +++++----- custom_components/battery_sim/sensor.py | 79 ++++++++--------------- 2 files changed, 46 insertions(+), 67 deletions(-) diff --git a/custom_components/battery_sim/__init__.py b/custom_components/battery_sim/__init__.py index 93ff288..eb4e61f 100644 --- a/custom_components/battery_sim/__init__.py +++ b/custom_components/battery_sim/__init__.py @@ -207,21 +207,24 @@ def __init__(self, config, hass): }, ] if len(config.get(CONF_SECOND_IMPORT_SENSOR, "")) > 6: - self._inputs[2] = { + self._inputs.append({ SENSOR_ID: config[CONF_SECOND_IMPORT_SENSOR], SENSOR_TYPE: IMPORT, SIMULATED_SENSOR: GRID_SECOND_IMPORT_SIM, TARIFF_TYPE: self._tariff_type, TARIFF: None - } + }) if len(config.get(CONF_SECOND_EXPORT_SENSOR, "")) > 6: - self._inputs[3] = { + self._inputs.append({ SENSOR_ID: config[CONF_SECOND_EXPORT_SENSOR], SENSOR_TYPE: EXPORT, SIMULATED_SENSOR: GRID_SECOND_EXPORT_SIM, TARIFF_TYPE: self._tariff_type, TARIFF: None - } + }) + + self._last_import_reading_sensor_data = self._inputs[0] + self._last_export_reading_sensor_data = self._inputs[1] """Default sensor entities for backwards compatibility""" if CONF_ENERGY_IMPORT_TARIFF in config: @@ -335,18 +338,13 @@ def reset_sim_sensor( def async_source_tracking(self, event): """Wait for source to be ready, then start.""" - reading_function=self.async_reading_handler - collection="_sensor_collection", for input_details in self._inputs: """Start tracking state changes for a sensor.""" - getattr(self, collection).append( - async_track_state_change_event( - self._hass, - [input_details[SENSOR_ID]], - lambda event: reading_function(event, input_details), - ) + listener_handle = async_track_state_change_event( + self._hass, + [input_details[SENSOR_ID]], + self.async_reading_handler ) - _LOGGER.debug("(%s) monitoring %s", self._name, input_details[SENSOR_ID]) _LOGGER.debug( @@ -358,16 +356,20 @@ def async_source_tracking(self, event): def async_reading_handler( self, event, - input_details ): + sensor_id = event.data.get("entity_id") + for input_details in self._inputs: + if sensor_id == input_details[SENSOR_ID]: break + else: + _LOGGER.warning(f"Error reading input sensor {sensor_id} not found in input sensors") + return + """Handle the sensor state changes for import or export.""" sensor_charge_rate = DISCHARGING_RATE if input_details[SENSOR_TYPE] == IMPORT else CHARGING_RATE - last_reading_time = time.time() old_state = event.data.get("old_state") new_state = event.data.get("new_state") - sensor_id = event.data.get("entity_id") if ( old_state is None diff --git a/custom_components/battery_sim/sensor.py b/custom_components/battery_sim/sensor.py index c41018b..faaff25 100644 --- a/custom_components/battery_sim/sensor.py +++ b/custom_components/battery_sim/sensor.py @@ -41,6 +41,7 @@ GRID_EXPORT_SIM, GRID_SECOND_EXPORT_SIM, GRID_SECOND_IMPORT_SIM, + SIMULATED_SENSOR, ICON_CHARGING, ICON_DISCHARGING, ICON_FULL, @@ -54,6 +55,7 @@ MESSAGE_TYPE_BATTERY_RESET_IMP, MESSAGE_TYPE_BATTERY_RESET_EXP, MESSAGE_TYPE_BATTERY_UPDATE, + SENSOR_ID ) _LOGGER = logging.getLogger(__name__) @@ -125,36 +127,11 @@ async def define_sensors(hass, handle): UnitOfPower.KILO_WATT ) ) - sensors.append( - DisplayOnlySensor( - handle, - GRID_EXPORT_SIM, - SensorDeviceClass.ENERGY, - UnitOfEnergy.KILO_WATT_HOUR, - ) - ) - sensors.append( - DisplayOnlySensor( - handle, - GRID_IMPORT_SIM, - SensorDeviceClass.ENERGY, - UnitOfEnergy.KILO_WATT_HOUR, - ) - ) - if handle._second_export_sensor_id is not None: + for input in handle._inputs: sensors.append( DisplayOnlySensor( handle, - GRID_SECOND_EXPORT_SIM, - SensorDeviceClass.ENERGY, - UnitOfEnergy.KILO_WATT_HOUR, - ) - ) - if handle._second_import_sensor_id is not None: - sensors.append( - DisplayOnlySensor( - handle, - GRID_SECOND_IMPORT_SIM, + input[SIMULATED_SENSOR], SensorDeviceClass.ENERGY, UnitOfEnergy.KILO_WATT_HOUR, ) @@ -168,33 +145,31 @@ async def define_sensors(hass, handle): None) ) - if handle._import_tariff_sensor_id is not None: - sensors.append( - DisplayOnlySensor( - handle, - ATTR_MONEY_SAVED_IMPORT, - SensorDeviceClass.MONETARY, - hass.config.currency, - ) + sensors.append( + DisplayOnlySensor( + handle, + ATTR_MONEY_SAVED_IMPORT, + SensorDeviceClass.MONETARY, + hass.config.currency, ) - sensors.append( - DisplayOnlySensor( - handle, - ATTR_MONEY_SAVED, - SensorDeviceClass.MONETARY, - hass.config.currency, - ) + ) + sensors.append( + DisplayOnlySensor( + handle, + ATTR_MONEY_SAVED, + SensorDeviceClass.MONETARY, + hass.config.currency, ) + ) - if handle._export_tariff_sensor_id is not None: - sensors.append( - DisplayOnlySensor( - handle, - ATTR_MONEY_SAVED_EXPORT, - SensorDeviceClass.MONETARY, - hass.config.currency, - ) + sensors.append( + DisplayOnlySensor( + handle, + ATTR_MONEY_SAVED_EXPORT, + SensorDeviceClass.MONETARY, + hass.config.currency, ) + ) sensors.append(SimulatedBattery(handle)) sensors.append(BatteryStatus(handle, BATTERY_MODE)) return sensors @@ -446,6 +421,8 @@ def unit_of_measurement(self): @property def extra_state_attributes(self): """Return the state attributes of the sensor.""" + for input in self.handle._inputs: + sensor_list = input[SENSOR_ID] return { ATTR_STATUS: self.handle._sensors[BATTERY_MODE], @@ -462,7 +439,7 @@ def extra_state_attributes(self): CONF_BATTERY_MAX_CHARGE_RATE: float(self.handle._max_charge_rate), ATTR_SOURCE_ID: - self.handle._export_sensor_id, + sensor_list, } @property