Skip to content

Commit

Permalink
Merge pull request #77 from RustyDust/serial_hotfix
Browse files Browse the repository at this point in the history
Hotfix for missing serial number, closes #76
  • Loading branch information
RustyDust authored Jan 15, 2025
2 parents b82a963 + 5c1536d commit a76943f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 22 additions & 1 deletion custom_components/sonnenbatterie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_IP_ADDRESS
from homeassistant.core import (
HomeAssistant, SupportsResponse,
)
from homeassistant.exceptions import ConfigEntryNotReady
from sonnenbatterie import AsyncSonnenBatterie

from .const import *
from .coordinator import SonnenbatterieCoordinator
Expand Down Expand Up @@ -46,6 +48,15 @@
)


async def _get_serial_number(config_entry: ConfigEntry) -> str:
sb_conn = AsyncSonnenBatterie(config_entry.data.get(CONF_USERNAME),
config_entry.data.get(CONF_PASSWORD),
config_entry.data.get(CONF_IP_ADDRESS))
await sb_conn.logout()
sysdata = await sb_conn.get_systemdata()
await sb_conn.logout()
return sysdata.get("DE_Ticket_Number", "sru-unknown")

# noinspection PyUnusedLocal
async def async_setup(hass, config):
"""Set up using YAML is not supported by this integration."""
Expand All @@ -58,9 +69,19 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if DOMAIN not in hass.data:
hass.data.setdefault(DOMAIN, {})

# Fix missing serial number
serial_number = config_entry.data.get(CONF_SERIAL_NUMBER)
if serial_number is None:
LOGGER.debug("No serial number provided, trying to determine from Sonnenbatterie")
serial_number = await _get_serial_number(config_entry)
_config_data = config_entry.data.copy()
_config_data[CONF_SERIAL_NUMBER] = serial_number
hass.config_entries.async_update_entry(config_entry, data=_config_data)
LOGGER.debug(f"serial_number: {serial_number}")

# init the master coordinator
sb_coordinator = SonnenbatterieCoordinator(hass, config_entry, serial_number)

sb_coordinator = SonnenbatterieCoordinator(hass, config_entry)
# calls SonnenbatterieCoordinator._async_update_data()
await sb_coordinator.async_refresh()
if not sb_coordinator.last_update_success:
Expand Down
12 changes: 7 additions & 5 deletions custom_components/sonnenbatterie/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
class SonnenbatterieCoordinator(DataUpdateCoordinator):
"""Class to manage fetching Sonnenbatteries data."""

def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
LOGGER.debug(f"Initializing SonnenbatterieCoordinator: {config_entry.data}")
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, serial: str) -> None:
LOGGER.info(f"Initializing SonnenbatterieCoordinator: {config_entry.data}")

""" private attributes """
self._batt_reserved_factor = 7.0 # fixed value, reseved percentage of total installed power for internal use
Expand All @@ -28,10 +28,11 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
""" public attributes """
self.latestData = {}
self.name = config_entry.title
self.serial = config_entry.data.get(CONF_SERIAL_NUMBER, "unknown")
self.serial = serial
self.sbconn = AsyncSonnenBatterie(username=self._config_entry.data[CONF_USERNAME],
password=self._config_entry.data[CONF_PASSWORD],
ipaddress=self._config_entry.data[CONF_IP_ADDRESS])

super().__init__(hass,
LOGGER,
name=DOMAIN,
Expand All @@ -41,14 +42,15 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
def device_info(self) -> DeviceInfo:
system_data = self.latestData["battery_system"]["battery_system"]
system_info = self.latestData["system_data"]

# noinspection HttpUrlsUsage
return DeviceInfo(
identifiers={(DOMAIN, self._config_entry.entry_id)},
configuration_url=f"http://{self._config_entry.data[CONF_IP_ADDRESS]}/",
manufacturer="Sonnen",
model=system_info.get("ERP_ArticleName", "unknown"),
name=f"{DOMAIN} {self._config_entry.data.get(CONF_SERIAL_NUMBER, 'unknown')}",
serial_number=f"{self._config_entry.data[CONF_SERIAL_NUMBER]}",
name=f"{DOMAIN} {self.serial}",
serial_number=f"{self.serial}",
sw_version=f"{system_data['software'].get('software_version', 'unknown')} ({system_data['software'].get('firmware_version', 'unknown')})",
hw_version=f"{system_data['system'].get('hardware_version', 'unknown'):.1f}",
)
Expand Down

0 comments on commit a76943f

Please sign in to comment.