Skip to content

Commit

Permalink
feat: repair serial number not being unique
Browse files Browse the repository at this point in the history
  • Loading branch information
mukaschultze committed Feb 6, 2025
1 parent cf40790 commit 4b89da3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
13 changes: 12 additions & 1 deletion custom_components/must_inverter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.event import async_track_time_interval
from pymodbus.client import AsyncModbusSerialClient, AsyncModbusTcpClient, AsyncModbusUdpClient

Expand All @@ -24,7 +25,6 @@

from .const import (
DOMAIN,
DEFAULT_SCAN_INTERVAL,
CONF_BAUDRATE,
CONF_PARITY,
CONF_STOPBITS,
Expand Down Expand Up @@ -359,6 +359,17 @@ async def read_modbus_data(self):
self._reading = False
# _LOGGER.debug("Data: %s", self.data)

if self.data["InverterSerialNumber"] == 0xFFFFFFFF or self.data["InverterSerialNumber"] == 0:
ir.async_create_issue(
self._hass,
DOMAIN,
"no_serial_number",
is_fixable=True,
severity=ir.IssueSeverity.WARNING,
translation_key="no_serial_number",
data={"entry_id": self._entry.entry_id},
)

return True

def _device_info(self):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/must_inverter/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def time(address, registers):


def serial(address, registers):
return hex(registers[address] << 16 | registers[address + 1])
return registers[address] << 16 | registers[address + 1]


def model(address, registers):
Expand Down
60 changes: 60 additions & 0 deletions custom_components/must_inverter/repairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import annotations

import voluptuous as vol
import time

from homeassistant import data_entry_flow
from homeassistant.components.repairs import RepairsFlow
from homeassistant.core import HomeAssistant

from .__init__ import MustInverter
from .const import DOMAIN


class NoSerialNumber(RepairsFlow):
"""Handler for an issue fixing flow."""

def __init__(self, hass: HomeAssistant, data: dict[str, str | int | float | None] | None) -> None:
"""Initialize the flow."""
self._hass = hass
self._data = data

async def async_step_init(self, user_input: dict[str, str] | None = None) -> data_entry_flow.FlowResult:
"""Handle the first step of a fix flow."""
return await self.async_step_confirm()

async def async_step_confirm(self, user_input: dict[str, str] | None = None) -> data_entry_flow.FlowResult:
"""Handle the confirm step of a fix flow."""
if user_input is not None:
entry_id = self._data["entry_id"]
device: MustInverter = self._hass.data[DOMAIN][entry_id]["inverter"]
new_serial_number = user_input["new_serial"]

lower = new_serial_number & 0xFFFF
upper = (new_serial_number >> 16) & 0xFFFF

await device.write_modbus_data(20002, upper)
await device.write_modbus_data(20003, lower)

self._hass.config_entries.async_schedule_reload(entry_id)

return self.async_create_entry(title="", data={})

return self.async_show_form(
step_id="confirm",
data_schema=vol.Schema(
{
vol.Required("new_serial", default=int(time.time())): int,
}
),
)


async def async_create_fix_flow(
hass: HomeAssistant,
issue_id: str,
data: dict[str, str | int | float | None] | None,
) -> RepairsFlow:
"""Create flow."""
if issue_id == "no_serial_number":
return NoSerialNumber(hass, data)
20 changes: 20 additions & 0 deletions custom_components/must_inverter/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@
}
}
},
"issues": {
"no_serial_number": {
"title": "No serial number set",
"fix_flow": {
"step": {
"confirm": {
"title": "No serial number set",
"description": "Your inverter does not have a serial number written to it. This will cause issues with the integration when attempting to add multiple inverters.\n\nThis repair will write a serial number to the inverter. This will not affect the operation of the inverter, but it will allow the integration to work correctly with more than one device simultaneously.",
"data": {
"new_serial": "Serial number to set",
"confirm": "I understand and want to proceed with the repair"
},
"data_description": {
"new_serial": "You can find your serial number written on the side of the device. Otherwise, you can leave this unchanged to use a random value."
}
}
}
}
}
},
"selector": {
"model": {
"options": {
Expand Down

0 comments on commit 4b89da3

Please sign in to comment.