From f4e667acb3e564832bfd0eda42650b582a3d3a23 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Sun, 18 Jun 2023 09:58:27 +0000 Subject: [PATCH] With power fixes --- .devcontainer/configuration.yaml | 22 +++++++++++++------ .../solar_optimizer/coordinator.py | 4 +++- .../solar_optimizer/managed_device.py | 2 ++ custom_components/solar_optimizer/switch.py | 6 +++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.devcontainer/configuration.yaml b/.devcontainer/configuration.yaml index e75a80c..d88e18d 100644 --- a/.devcontainer/configuration.yaml +++ b/.devcontainer/configuration.yaml @@ -123,19 +123,27 @@ input_number: mode: slider step: 1 +template: + - trigger: + - platform: time_pattern + # This will update every night + seconds: "/2" + sensor: + - name: "Next availability Tesla (sec)" + state: "{{ (as_timestamp(state_attr('switch.solar_optimizer_equipement_h', 'next_date_available')) - as_timestamp(now())) | int(0) }}" + unit_of_measurement: "s" + state_class: measurement + - name: "Next availability Tesla Power (sec)" + state: "{{ (as_timestamp(state_attr('switch.solar_optimizer_equipement_h', 'next_date_available_power')) - as_timestamp(now())) | int(0) }}" + unit_of_measurement: "s" + state_class: measurement solar_optimizer: - refresh_period_sec: 10 algorithm: initial_temp: 1000 min_temp: 0.1 cooling_factor: 0.95 max_iteration_number: 1000 - power_consumption_entity_id: "input_number.consommation_net" - power_production_entity_id: "input_number.production_solaire" - sell_cost_entity_id: "input_number.sell_cost" - buy_cost_entity_id: "input_number.buy_cost" - sell_tax_percent_entity_id: "input_number.sell_tax_percent" devices: - name: "Equipement A" entity_id: "input_boolean.fake_device_a" @@ -189,7 +197,7 @@ solar_optimizer: entity_id: "input_boolean.fake_device_g" power_max: 1200 check_usable_template: "{{ True }}" - duration_min: 90 + duration_min: 1.5 action_mode: "service_call" activation_service: "input_boolean/turn_on" deactivation_service: "input_boolean/turn_off" diff --git a/custom_components/solar_optimizer/coordinator.py b/custom_components/solar_optimizer/coordinator.py index 33de364..163e823 100644 --- a/custom_components/solar_optimizer/coordinator.py +++ b/custom_components/solar_optimizer/coordinator.py @@ -140,7 +140,6 @@ async def _async_update_data(self): requested_power = equipement.get("requested_power") state = equipement["state"] device = self.get_device_name(name) - calculated_data[name_to_unique_id(name)] = device if not device: continue is_active = device.is_active @@ -167,6 +166,9 @@ async def _async_update_data(self): should_log = True await device.change_requested_power(requested_power) + # Add updated data to the result + calculated_data[name_to_unique_id(name)] = device + if should_log: _LOGGER.info("Calculated data are: %s", calculated_data) else: diff --git a/custom_components/solar_optimizer/managed_device.py b/custom_components/solar_optimizer/managed_device.py index d5de3e2..2d2cc3c 100644 --- a/custom_components/solar_optimizer/managed_device.py +++ b/custom_components/solar_optimizer/managed_device.py @@ -188,6 +188,8 @@ async def _apply_action(self, action_type: str, requested_power=None): if action_type == ACTION_ACTIVATE: method = self._activation_service self.reset_next_date_available() + if self._can_change_power: + self.reset_next_date_available_power() elif action_type == ACTION_DEACTIVATE: method = self._deactivation_service self.reset_next_date_available() diff --git a/custom_components/solar_optimizer/switch.py b/custom_components/solar_optimizer/switch.py index a0f127e..1d3542e 100644 --- a/custom_components/solar_optimizer/switch.py +++ b/custom_components/solar_optimizer/switch.py @@ -67,9 +67,11 @@ def __init__(self, coordinator, hass, name, idx, entity_id): self._entity_id = entity_id # Try to get the state if it exists - device: ManagedDevice = coordinator.data.get(self.idx) - if device: + device: ManagedDevice = None + if coordinator.data and (device := coordinator.data.get(self.idx)) is not None: self._attr_is_on = device.is_active + else: + self._attr_is_on = None async def async_added_to_hass(self) -> None: """The entity have been added to hass, listen to state change of the underlying entity"""