From a866e523b81d47c9690f04cb152bf00f0dd64e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kr=C3=BCger?= Date: Fri, 13 Dec 2024 11:33:18 +0100 Subject: [PATCH] fix: minor bug in _async_find_best_time_to_charge (#75) --- ...> license-compliance-checker.yml.disabled} | 2 +- .gitignore | 1 + custom_components/hsem/const.py | 4 +- .../custom_sensors/working_mode_sensor.py | 62 +++++++------------ requirements.txt | 3 + 5 files changed, 30 insertions(+), 42 deletions(-) rename .github/workflows/{license-compliance-checker.yml => license-compliance-checker.yml.disabled} (92%) diff --git a/.github/workflows/license-compliance-checker.yml b/.github/workflows/license-compliance-checker.yml.disabled similarity index 92% rename from .github/workflows/license-compliance-checker.yml rename to .github/workflows/license-compliance-checker.yml.disabled index 9fcfa15..e4a90eb 100644 --- a/.github/workflows/license-compliance-checker.yml +++ b/.github/workflows/license-compliance-checker.yml.disabled @@ -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 }}" diff --git a/.gitignore b/.gitignore index 47c1111..dfd6252 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ coverage.xml notes.txt .venv .DS_Store +sync.sh # Home Assistant configuration config/* diff --git a/custom_components/hsem/const.py b/custom_components/hsem/const.py index af3c0cf..2c90769 100644 --- a/custom_components/hsem/const.py +++ b/custom_components/hsem/const.py @@ -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/-", ] diff --git a/custom_components/hsem/custom_sensors/working_mode_sensor.py b/custom_components/hsem/custom_sensors/working_mode_sensor.py index 7d63f8b..8fca435 100644 --- a/custom_components/hsem/custom_sensors/working_mode_sensor.py +++ b/custom_components/hsem/custom_sensors/working_mode_sensor.py @@ -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) @@ -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 @@ -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 @@ -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: @@ -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 @@ -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): """ @@ -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}% " diff --git a/requirements.txt b/requirements.txt index 35fa722..00028ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ voluptuous==0.15.2 pytz==2024.2 +homeassistant==2024.12.0 +black==24.8.0 +isort==5.11.5