Skip to content

Commit

Permalink
Merge pull request #6 from MarkusBiggus/dev
Browse files Browse the repository at this point in the history
refactor some responsibilities from sonnen_api_v2 for ha component
  • Loading branch information
MarkusBiggus authored Dec 4, 2024
2 parents c894d87 + aed655c commit d681971
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 206 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# python_sonnenbatterie compatible (readonly) wrapper for python_sonnnen_api_v2
# python_sonnenbatterie compatible (readonly) wrapper for package sonnnen_api_v2
Install this wrapper package with sonnen_api_v2 package to use Home Assistant custom component "sonnenbatterie" by weltmeyer.

sonnen_api_v2 uses only the API read token from sonnen batterie admin portal. Differs from weltmeter package in that it does not use the default user login older batterie's have setup.
sonnen_api_v2 uses only the API read token from sonnen batterie admin portal.

Differs from weltmeter package in that it does not use the default user login setup in older batteries.

There are no API V1 calls, response is faked using API V2 results.
Notably, serial number and model number are not avaiable from any API V2 endpoint.

Notably, serial number and model number are not available from any API V2 endpoint.

pip install "git+https://github.com/MarkusBiggus/sonnen_api_v2.git"

Expand Down
35 changes: 34 additions & 1 deletion sonnenbatterie/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
""" SonnenBatterie API V2 component """
""" SonnenBatterie API V2 package """
from .sonnenbatterie import sonnenbatterie
__all__ = (
"set_login_timeout",
"get_login_timeout",
"set_request_connect_timeout",
"get_request_connect_timeout",
"set_request_read_timeout",
"get_request_read_timeout",
"get_powermeter",
"get_batterysystem",
"get_inverter",
"get_systemdata",
"get_status",
"get_battery",
"get_latest_data",
"get_configurations",
"get_configuration",
"get_current_charge_level",
"get_operating_mode",
"get_operating_mode_name",
"get_battery_reserve",
"get_time_of_use_schedule_as_string",
"get_time_of_use_schedule_as_json_objects",
"get_time_of_use_schedule_as_schedule",
"set_manual_flowrate",
"set_discharge",
"set_charge",
"set_configuration",
"set_operating_mode",
"set_operating_mode_by_name",
"set_battery_reserve",
"set_battery_reserve_relative_to_currentCharge",
"set_time_of_use_schedule_from_json_objects",
)
8 changes: 4 additions & 4 deletions sonnenbatterie/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from datetime import time
#from requests import Timeout

import logging

DEFAULT_BATTERY_LOGIN_TIMEOUT=120
DEFAULT_CONNECT_TO_BATTERY_TIMEOUT=60
Expand Down Expand Up @@ -32,4 +30,6 @@
SONNEN_API_PATH_BATTERY="battery"

SONNEN_CHARGE_PATH="charge"
SONNEN_DISCHARGE_PATH="discharge"
SONNEN_DISCHARGE_PATH="discharge"

LOGGER = logging.getLogger(__package__)
77 changes: 62 additions & 15 deletions sonnenbatterie/sonnenbatterie.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import os
#import os
#from dotenv import load_dotenv
#import hashlib
from typing import Union
import requests
import json
# pylint: disable=unused-wildcard-import
from .const import *
# pylint: enable=unused-wildcard-import
from .timeofuse import timeofuseschedule
from sonnen_api_v2.sonnen import Sonnen as Batterie
from dotenv import load_dotenv
from sonnen_api_v2.sonnen import Sonnen as Batterie, BatterieError

# indexes for _batteryRequestTimeout
TIMEOUT_CONNECT=0
TIMEOUT_REQUEST=1

load_dotenv()
#load_dotenv()

class sonnenbatterie:

def __init__(self,username,token,ipaddress) -> None:
def __init__(self, username, token, ipaddress) -> None:
# self.username=username
self.token=token
self.ipaddress=ipaddress
Expand All @@ -29,9 +27,13 @@ def __init__(self,username,token,ipaddress) -> None:
# self._batteryRequestTimeout = (self._batteryConnectTimeout, self._batteryReadTimeout)
self._batteryRequestTimeout = (DEFAULT_CONNECT_TO_BATTERY_TIMEOUT, DEFAULT_READ_FROM_BATTERY_TIMEOUT)
self.batterie = Batterie(self.token, self.ipaddress)
batterie_status = self.batterie.get_status()
if batterie_status is False:
LOGGER.error('Unable to connect to sonnenbatterie!')
raise BatterieError('Unable to connect to sonnenbatterie!')
self.batterie.set_request_connect_timeouts(self._batteryRequestTimeout)
self._battery_serial_number = os.getenv("BATTERIE_SN", "unknown")
self._battery_model = os.getenv("BATTERIE_MODEL", "unknown")
self._battery_serial_number = 'unknown' #os.getenv("BATTERIE_SN", "unknown")
self._battery_model = 'unknown' #os.getenv("BATTERIE_MODEL", "unknown")

# self._login()

Expand Down Expand Up @@ -142,10 +144,6 @@ def get_powermeter(self):
# return self._get(SONNEN_API_PATH_POWER_METER)
return self.batterie.get_powermeter()

def get_batterysystem(self):
'''battery_system not in V2 - fake it for required component attributes'''
return self.batterie.get_batterysystem()

def get_inverter(self):
# return self._get(SONNEN_API_PATH_INVERTER)
return self.batterie.get_inverter()
Expand All @@ -160,13 +158,62 @@ def get_systemdata(self):
# return self._get(SONNEN_API_PATH_SYSTEM_DATA)
return systemdata

def get_batterysystem(self)-> Union[str, bool]:
"""battery_system not in V2 - fake it for required component attributes"""

configurations_data = self.batterie.get_configurations()
if configurations_data is None:
return None
systemdata = {'modules':
configurations_data.get('IC_BatteryModules'),
'battery_system':
{
'system':
{
'storage_capacity_per_module': configurations_data.get('CM_MarketingModuleCapacity'),
'depthofdischargelimit': configurations_data.get('DepthOfDischargeLimit'),
}
}

}
return systemdata

def get_status(self):
# return self._get(SONNEN_API_PATH_STATUS)
return self.batterie.get_status()

def get_battery(self):
"""Battery status for sonnenbatterie wrapper
Fake V1 API data used by ha sonnenbatterie component
Returns:
json response
"""
# return self._get(SONNEN_API_PATH_BATTERY)
return self.batterie.get_battery()
battery_status = self.batterie.get_battery()

configurations_data = self.batterie.get_configurations()
if configurations_data is None:
return None

""" current_state index of: ["standby", "charging", "discharging", "charged", "discharged"] """
if self.batterie.status_battery_charging:
battery_status['current_state'] = "charging"
elif self.batterie.status_battery_discharging:
battery_status['current_state'] = "discharging"
elif self.batterie.battery_rsoc > 98:
battery_status['current_state'] = "charged"
elif self.batterie.battery_usable_remaining_capacity < 2:
battery_status['current_state'] = "discharged"
else:
battery_status['current_state'] = "standby"

measurements = {'battery_status': {'cyclecount': self.batterie.battery_cycle_count,
'stateofhealth': int(battery_status.get('systemstatus'))
}
}
battery_status['measurements'] = measurements

return battery_status

def get_latest_data(self):
# return self._get(SONNEN_API_PATH_LATEST_DATA)
Expand Down
32 changes: 0 additions & 32 deletions test/mock_battery.py

This file was deleted.

25 changes: 0 additions & 25 deletions test/mock_configurations.py

This file was deleted.

18 changes: 0 additions & 18 deletions test/mock_inverter.py

This file was deleted.

49 changes: 0 additions & 49 deletions test/mock_powermeter.py

This file was deleted.

Loading

0 comments on commit d681971

Please sign in to comment.