Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamish Findlay committed Sep 9, 2023
1 parent ad30adc commit 438c414
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 67 deletions.
34 changes: 18 additions & 16 deletions custom_components/battery_sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
79 changes: 28 additions & 51 deletions custom_components/battery_sim/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
GRID_EXPORT_SIM,
GRID_SECOND_EXPORT_SIM,
GRID_SECOND_IMPORT_SIM,
SIMULATED_SENSOR,
ICON_CHARGING,
ICON_DISCHARGING,
ICON_FULL,
Expand All @@ -54,6 +55,7 @@
MESSAGE_TYPE_BATTERY_RESET_IMP,
MESSAGE_TYPE_BATTERY_RESET_EXP,
MESSAGE_TYPE_BATTERY_UPDATE,
SENSOR_ID
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -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
Expand Down

0 comments on commit 438c414

Please sign in to comment.