Skip to content

Commit

Permalink
Fixed issues
Browse files Browse the repository at this point in the history
Fixed issues where battery not accounting for empty state correctly
Added def state(self) as well as native_state(self) - not sure why this was wrong, but works now
Same for unit_of_measurement
Took so long to figure this out, don't understand as using SensorEntity object...
  • Loading branch information
hif2k1 authored Sep 6, 2021
1 parent b8f6845 commit 08bd0ce
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

async_add_entities(batteries)


class SimulatedBattery(RestoreEntity, SensorEntity):
"""Representation of an utility meter sensor."""

Expand Down Expand Up @@ -125,6 +124,7 @@ def __init__(

@callback
def async_export_reading(self, event):

"""Handle the sensor state changes."""
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
Expand All @@ -136,30 +136,36 @@ def async_export_reading(self, event):
):
return

if self._state=='unknown': self._state = 0

try:
"""Calculate maximum possible charge based on battery specifications"""
time_now = time.time()
time_since_last_import = time_now-self._last_import_reading_time
self._last_import_reading_time = time_now
max_charge = time_since_last_import*self._max_charge_rate/3600

diff = Decimal(new_state.state) - Decimal(old_state.state)
diff = float(new_state.state) - float(old_state.state)

if diff <= 0:
return

available_capacity = self._battery_size - float(self._state)

diff = min(diff, max_charge)
diff = min(diff, max_charge, available_capacity)

self._state = min(self._battery_size, self._state + diff)
self._state = round(float(self._state) + diff,2)
self._charging = True
self._charge_percentage = round(100*float(self._state)/float(self._battery_size))

except ValueError as err:
_LOGGER.warning("While processing state changes: %s", err)
except DecimalException as err:
_LOGGER.warning(
"Invalid state (%s > %s): %s", old_state.state, new_state.state, err
)
self.async_write_ha_state()
self.schedule_update_ha_state(True)
# self.async_write_ha_state()

@callback
def async_import_reading(self, event):
Expand All @@ -174,23 +180,25 @@ def async_import_reading(self, event):
):
return

if self._state=='unknown': self._state = 0

try:
"""Calculate maximum possible discharge based on battery specifications"""
time_now = time.time()
time_since_last_import = time_now-self._last_import_reading_time
self._last_import_reading_time = time_now
max_discharge = time_since_last_import*self._max_discharge_rate/3600

diff = Decimal(new_state.state) - Decimal(old_state.state)

diff = float(new_state.state) - float(old_state.state)
_LOGGER.warning("State3: (%s)", self._state)
if diff <= 0:
return

diff = min(diff, max_discharge)
self._state = max(0, Decimal(self._state) - diff/Decimal(self._battery_efficiency))
diff = min(diff, max_discharge, float(self._state)*float(self._battery_efficiency))

self._state = round(float(self._state) - diff/float(self._battery_efficiency),2)
self._energy_saved += diff
self._charge_percentage = round(100*Decimal(self._state)/Decimal(self._battery_size))
self._charge_percentage = round(100*float(self._state)/float(self._battery_size))

self._charging = False

Expand All @@ -200,15 +208,19 @@ def async_import_reading(self, event):
_LOGGER.warning(
"Invalid state (%s > %s): %s", old_state.state, new_state.state, err
)
self.async_write_ha_state()
# self.async_write_ha_state()
self.schedule_update_ha_state(True)
_LOGGER.warning("State 4: ")

async def async_added_to_hass(self):
"""Handle entity which will be added."""
await super().async_added_to_hass()

state = await self.async_get_last_state()
if state:
self._state = Decimal(state.state)
self._state = state.state

# async_dispatcher_connect(self.hass)

@callback
def async_source_tracking(event):
Expand All @@ -235,6 +247,7 @@ def name(self):
@property
def native_value(self):
"""Return the state of the sensor."""
_LOGGER.warning("State7: (%s)", str(self._state))
return self._state

@property
Expand All @@ -254,6 +267,11 @@ def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return ENERGY_KILO_WATT_HOUR

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return ENERGY_KILO_WATT_HOUR

@property
def should_poll(self):
"""No polling needed."""
Expand Down Expand Up @@ -281,3 +299,13 @@ def icon(self):
return ICON_CHARGING
else:
return ICON_DISCHARGING

@property
def state(self):
"""Return the state of the sensor."""
return self._state

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""

0 comments on commit 08bd0ce

Please sign in to comment.