Skip to content

Commit

Permalink
FIX init current_power
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Jun 17, 2023
1 parent aeb130a commit 01d117c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
20 changes: 2 additions & 18 deletions custom_components/solar_optimizer/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,10 @@ async def _async_update_data(self):

calculated_data = {}

# device_states = {}
# Add a device state attributes
for _, device in enumerate(self._devices):
# Initialize current power if not set and is active
if device.is_active and device.current_power == 0:
power = device.power_max
if device.can_change_power:
state = self.hass.states.get(device.power_entity_id)
if power is not None:
power = round(
float(state.state) * device.convert_power_divide_factor
)
else:
power = device.power_min
device.reset_next_date_available()
device.reset_next_date_available_power()

device.init_power(power)
if not device.is_active:
device.init_power(0)
# Initialize current power depending or reality
device.set_current_power_with_device_state()

# Add a power_consumption and power_production
calculated_data["power_production"] = get_safe_float(
Expand Down
49 changes: 44 additions & 5 deletions custom_components/solar_optimizer/managed_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from homeassistant.core import HomeAssistant
from homeassistant.helpers.template import Template
from homeassistant.const import STATE_ON
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN

from .const import (
get_tz,
Expand Down Expand Up @@ -256,12 +256,51 @@ def reset_next_date_available_power(self):
self._next_date_available_power,
)

def init_power(self, power: int):
"""Initialise current_power and requested_power to the given value"""
# def init_power(self, power: int):
# """Initialise current_power and requested_power to the given value"""
# _LOGGER.debug(
# "Initializing power for entity '%s' with %s value", self._name, power
# )
# self._requested_power = self._current_power = power

def set_current_power_with_device_state(self):
"""Set the current power according to the real device state"""
if not self.is_active:
self._current_power = 0
_LOGGER.debug(
"Set current_power to 0 for device %s cause not active", self._name
)
return

if not self._can_change_power:
self._current_power = self._power_max
_LOGGER.debug(
"Set current_power to %s for device %s cause active and not can_change_power",
self._current_power,
self._name,
)
return

amps = self._hass.states.get(self._power_entity_id)
if not amps or amps.state in [None, STATE_UNKNOWN, STATE_UNAVAILABLE]:
self._current_power = self._power_min
_LOGGER.debug(
"Set current_power to %s for device %s cause can_change_power but amps is %s",
self._current_power,
self._name,
amps,
)
return

self._current_power = round(
float(amps.state) * self._convert_power_divide_factor
)
_LOGGER.debug(
"Initializing power for entity '%s' with %s value", self._name, power
"Set current_power to %s for device %s cause can_change_power and amps is %s",
self._current_power,
self._name,
amps.state,
)
self._requested_power = self._current_power = power

@property
def is_active(self):
Expand Down

0 comments on commit 01d117c

Please sign in to comment.