Skip to content

Commit

Permalink
With power fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Jun 18, 2023
1 parent cb8e3a4 commit f4e667a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
22 changes: 15 additions & 7 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion custom_components/solar_optimizer/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/solar_optimizer/managed_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions custom_components/solar_optimizer/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit f4e667a

Please sign in to comment.