Skip to content

Commit

Permalink
fix: minor bug in _async_find_best_time_to_charge (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
woopstar authored Dec 13, 2024
1 parent 403d76f commit a866e52
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
requirements: 'requirements-all.txt'
fail: 'Copyleft'
exclude: '(?i)^(pylint|aio[-_]*).*'
exclude: '(?i)^(pylint|aio|homeassistant|black|isort[-_]*).*'
- name: Print report
if: ${{ always() }}
run: echo "${{ steps.license_check_report.outputs.report }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ coverage.xml
notes.txt
.venv
.DS_Store
sync.sh

# Home Assistant configuration
config/*
Expand Down
4 changes: 1 addition & 3 deletions custom_components/hsem/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

# Default TOU modes for solar energy consumption throughout the day
DEFAULT_HSEM_DEFAULT_TOU_MODES = [
# "00:01-05:59/1234567/+",
"06:00-10:00/1234567/-",
# "15:00-16:59/1234567/+",
"06:00-11:59/1234567/-",
"17:00-23:59/1234567/-",
]

Expand Down
62 changes: 24 additions & 38 deletions custom_components/hsem/custom_sensors/working_mode_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,10 @@ async def _async_handle_update(self, event):
)

# Calculate the remaining charge needed to reach full capacity (kWh)
self._hsem_batteries_remaining_charge = round(
self._hsem_batteries_remaining_charge = (
(100 - self._hsem_huawei_solar_batteries_state_of_capacity_state)
/ 100
* (self._hsem_batteries_rated_capacity_max_state / 1000),
2,
* (self._hsem_batteries_rated_capacity_max_state / 1000)
)

# Calculate the maximum charge allowed from the grid based on cutoff SOC (kWh)
Expand All @@ -420,25 +419,18 @@ async def _async_handle_update(self, event):

# Adjust remaining charge if it exceeds the max grid-allowed charge
if self._hsem_batteries_remaining_charge > max_allowed_grid_charge:
self._hsem_batteries_remaining_charge = round(
max_allowed_grid_charge, 2
)
self._hsem_batteries_remaining_charge = max_allowed_grid_charge

# Calculate usable capacity (kWh)
self._hsem_batteries_usable_capacity = round(
(self._hsem_batteries_rated_capacity_max_state / 1000)
- (self._hsem_batteries_rated_capacity_min_state / 1000),
2,
)
self._hsem_batteries_usable_capacity = (
self._hsem_batteries_rated_capacity_max_state / 1000
) - (self._hsem_batteries_rated_capacity_min_state / 1000)

# Calculate current capacity (kWh)
self._hsem_batteries_current_capacity = round(
(
self._hsem_huawei_solar_batteries_state_of_capacity_state
/ 100
* (self._hsem_batteries_rated_capacity_max_state / 1000)
),
2,
self._hsem_batteries_current_capacity = (
self._hsem_huawei_solar_batteries_state_of_capacity_state
/ 100
* (self._hsem_batteries_rated_capacity_max_state / 1000)
)

# reset the recommendations
Expand Down Expand Up @@ -782,8 +774,6 @@ async def _async_calculate_net_consumption(self):
self._hsem_house_consumption_power_state
- self._hsem_solar_production_power_state
)

self._hsem_net_consumption = round(self._hsem_net_consumption, 2)
else:
self._hsem_net_consumption = 0.0

Expand Down Expand Up @@ -1198,9 +1188,7 @@ async def _async_calculate_hourly_net_consumption(self):
if avg_house_consumption is None or solcast_pv_estimate is None:
estimated_net_consumption = 0.0
else:
estimated_net_consumption = round(
avg_house_consumption - solcast_pv_estimate, 2
)
estimated_net_consumption = avg_house_consumption - solcast_pv_estimate

# calculate the estimated net consumption
if time_range in self._hourly_calculations:
Expand Down Expand Up @@ -1315,27 +1303,22 @@ async def _async_find_best_time_to_charge(self, start_hour=14, stop_hour=17):
)

# Adjust energy to charge based on surplus power (net_consumption)
available_surplus = (
round(abs(net_consumption), 2) if net_consumption < 0 else 0
)
max_available_energy = round(
min(max_charge_per_hour, remaining_charge_needed + available_surplus), 2
available_surplus = abs(net_consumption) if net_consumption < 0 else 0
max_available_energy = min(
max_charge_per_hour, remaining_charge_needed + available_surplus
)

# Deduct surplus from the actual charge needed
actual_energy_to_charge = round(
max(0, max_available_energy - available_surplus), 2
)
actual_energy_to_charge = max(0, max_available_energy - available_surplus)

# Mark hour for charging
self._mark_hour_for_charging(time_range, actual_energy_to_charge, source)
charged_energy += actual_energy_to_charge
charged_energy = round(charged_energy, 2)
charged_energy += actual_energy_to_charge + available_surplus

_LOGGER.warning(
f"Marked hour {time_range} for charging using {source}. "
f"Surplus Used: {available_surplus} kWh, Energy Charged: {actual_energy_to_charge} kWh, "
f"Total Charged: {charged_energy} kWh."
f"Surplus Used: {round(available_surplus,2)} kWh, Energy Charged: {round(actual_energy_to_charge,2)} kWh, "
f"Total Charged: {round(charged_energy,2)} kWh."
)

# Calculate total solar surplus after the charging hours
Expand Down Expand Up @@ -1375,8 +1358,11 @@ def _calculate_solar_surplus(self, charging_hours):
if net_consumption < 0:
solar_surplus += abs(net_consumption)

_LOGGER.warning(f"Calculated solar surplus: {solar_surplus} kWh")
return round(solar_surplus, 2)
_LOGGER.warning(
f"Solar surplus after battery charge available: {solar_surplus} kWh"
)

return solar_surplus

async def _async_adjust_ac_charge_cutoff_soc(self, charged_energy, solar_surplus):
"""
Expand Down Expand Up @@ -1413,7 +1399,7 @@ async def _async_adjust_ac_charge_cutoff_soc(self, charged_energy, solar_surplus
adjusted_cutoff_soc = 100 - (surplus_ratio * (100 - min_cutoff_soc))

# Update internal state
self._hsem_ac_charge_cutoff_percentage = round(adjusted_cutoff_soc, 2)
self._hsem_ac_charge_cutoff_percentage = adjusted_cutoff_soc

_LOGGER.warning(
f"Adjusted AC Grid Charge Cutoff SoC: {self._hsem_ac_charge_cutoff_percentage}% "
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
voluptuous==0.15.2
pytz==2024.2
homeassistant==2024.12.0
black==24.8.0
isort==5.11.5

0 comments on commit a866e52

Please sign in to comment.