Skip to content

Commit 2977c47

Browse files
authored
Merge pull request #319 from sgryphon/bugfix/318-update-unit-id-label
Bugfix 318: Update unit id label
2 parents 764902c + 5b99ae5 commit 2977c47

16 files changed

Lines changed: 120 additions & 98 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
// See https://aka.ms/vscode-remote/devcontainer.json for format details.
22
{
3-
"image": "ludeeus/container:integration-debian",
3+
"image": "mcr.microsoft.com/devcontainers/python:3.13",
44
"name": "SunSpec integration development",
5-
"context": "..",
6-
"appPort": ["9123:8123"],
7-
"postCreateCommand": "container install",
8-
"extensions": [
9-
"ms-python.python",
10-
"github.vscode-pull-request-github",
11-
"ryanluker.vscode-coverage-gutters",
12-
"ms-python.vscode-pylance"
13-
],
14-
"settings": {
15-
"files.eol": "\n",
16-
"editor.tabSize": 4,
17-
"terminal.integrated.shell.linux": "/bin/bash",
18-
"python.pythonPath": "/usr/bin/python3",
19-
"python.analysis.autoSearchPaths": false,
20-
"python.linting.pylintEnabled": true,
21-
"python.linting.enabled": true,
22-
"python.formatting.provider": "black",
23-
"editor.formatOnPaste": false,
24-
"editor.formatOnSave": true,
25-
"editor.formatOnType": true,
26-
"files.trimTrailingWhitespace": true
5+
"postCreateCommand": "scripts/setup",
6+
"forwardPorts": [8123],
7+
"portsAttributes": {
8+
"8123": {
9+
"label": "Home Assistant",
10+
"onAutoForward": "notify"
11+
}
12+
},
13+
"customizations": {
14+
"vscode": {
15+
"extensions": [
16+
"charliermarsh.ruff",
17+
"github.vscode-pull-request-github",
18+
"ms-python.python",
19+
"ms-python.vscode-pylance",
20+
"ryanluker.vscode-coverage-gutters"
21+
],
22+
"settings": {
23+
"files.eol": "\n",
24+
"editor.tabSize": 4,
25+
"editor.formatOnPaste": true,
26+
"editor.formatOnSave": true,
27+
"editor.formatOnType": false,
28+
"files.trimTrailingWhitespace": true,
29+
"python.analysis.typeCheckingMode": "basic",
30+
"python.analysis.autoImportCompletions": true,
31+
"python.defaultInterpreterPath": "/usr/local/bin/python",
32+
"[python]": {
33+
"editor.defaultFormatter": "charliermarsh.ruff"
34+
}
35+
}
36+
}
2737
}
2838
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pythonenv*
55
venv
66
.venv
77
.direnv
8+
*.code-workspace

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"python.pythonPath": "venv/bin/python",
55
"files.associations": {
66
"*.yaml": "home-assistant"
7-
}
7+
},
8+
"python.testing.pytestArgs": [],
9+
"python.testing.unittestEnabled": false,
10+
"python.testing.pytestEnabled": true
811
}

custom_components/sunspec/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .const import CONF_HOST
2121
from .const import CONF_PORT
2222
from .const import CONF_SCAN_INTERVAL
23-
from .const import CONF_SLAVE_ID
23+
from .const import CONF_UNIT_ID
2424
from .const import DEFAULT_MODELS
2525
from .const import DOMAIN
2626
from .const import PLATFORMS
@@ -44,9 +44,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
4444

4545
host = entry.data.get(CONF_HOST)
4646
port = entry.data.get(CONF_PORT)
47-
slave_id = entry.data.get(CONF_SLAVE_ID, 1)
47+
unit_id = entry.data.get(CONF_UNIT_ID, 1)
4848

49-
client = SunSpecApiClient(host, port, slave_id, hass)
49+
client = SunSpecApiClient(host, port, unit_id, hass)
5050

5151
_LOGGER.debug("Setup conifg entry for SunSpec")
5252
coordinator = SunSpecDataUpdateCoordinator(hass, client=client, entry=entry)
@@ -117,7 +117,7 @@ def __init__(self, hass: HomeAssistant, client: SunSpecApiClient, entry) -> None
117117
scan_interval,
118118
entry.data.get(CONF_HOST),
119119
entry.data.get(CONF_PORT),
120-
entry.data.get(CONF_SLAVE_ID),
120+
entry.data.get(CONF_UNIT_ID),
121121
)
122122
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=scan_interval)
123123

custom_components/sunspec/api.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,15 @@ def progress(msg):
9797
class SunSpecApiClient:
9898
CLIENT_CACHE = {}
9999

100-
def __init__(
101-
self, host: str, port: int, slave_id: int, hass: HomeAssistant
102-
) -> None:
100+
def __init__(self, host: str, port: int, unit_id: int, hass: HomeAssistant) -> None:
103101
"""Sunspec modbus client."""
104102

105103
_LOGGER.debug("New SunspecApi Client")
106104
self._host = host
107105
self._port = port
108106
self._hass = hass
109-
self._slave_id = slave_id
110-
self._client_key = f"{host}:{port}:{slave_id}"
107+
self._unit_id = unit_id
108+
self._client_key = f"{host}:{port}:{unit_id}"
111109
self._lock = threading.Lock()
112110
self._reconnect = False
113111

@@ -183,14 +181,14 @@ def modbus_connect(self, config=None):
183181
use_config = SimpleNamespace(
184182
**(
185183
config
186-
or {"host": self._host, "port": self._port, "slave_id": self._slave_id}
184+
or {"host": self._host, "port": self._port, "unit_id": self._unit_id}
187185
)
188186
)
189187
_LOGGER.debug(
190-
f"Client connect to IP {use_config.host} port {use_config.port} slave id {use_config.slave_id} using timeout {TIMEOUT}"
188+
f"Client connect to IP {use_config.host} port {use_config.port} unit id {use_config.unit_id} using timeout {TIMEOUT}"
191189
)
192190
client = modbus_client.SunSpecModbusClientDeviceTCP(
193-
slave_id=use_config.slave_id,
191+
slave_id=use_config.unit_id,
194192
ipaddr=use_config.host,
195193
ipport=use_config.port,
196194
timeout=TIMEOUT,
@@ -202,7 +200,7 @@ def modbus_connect(self, config=None):
202200
client.connect()
203201
if not client.is_connected():
204202
raise ConnectionError(
205-
f"Failed to connect to {self._host}:{self._port} slave id {self._slave_id}"
203+
f"Failed to connect to {self._host}:{self._port} unit id {self._unit_id}"
206204
)
207205
_LOGGER.debug("Client connected, perform initial scan")
208206
client.scan(
@@ -211,7 +209,7 @@ def modbus_connect(self, config=None):
211209
return client
212210
except ModbusClientError:
213211
raise ConnectionError(
214-
f"Failed to connect to {use_config.host}:{use_config.port} slave id {use_config.slave_id}"
212+
f"Failed to connect to {use_config.host}:{use_config.port} unit id {use_config.unit_id}"
215213
)
216214
else:
217215
_LOGGER.debug("Inverter not ready for Modbus TCP connection")

custom_components/sunspec/config_flow.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .const import CONF_PORT
1515
from .const import CONF_PREFIX
1616
from .const import CONF_SCAN_INTERVAL
17-
from .const import CONF_SLAVE_ID
17+
from .const import CONF_UNIT_ID
1818
from .const import DEFAULT_MODELS
1919
from .const import DOMAIN
2020

@@ -37,15 +37,15 @@ async def async_step_user(self, user_input=None):
3737
if user_input is not None:
3838
host = user_input[CONF_HOST]
3939
port = user_input[CONF_PORT]
40-
slave_id = user_input[CONF_SLAVE_ID]
41-
valid = await self._test_connection(host, port, slave_id)
40+
unit_id = user_input[CONF_UNIT_ID]
41+
valid = await self._test_connection(host, port, unit_id)
4242
if valid:
4343
uid = self._device_info.getValue("SN")
4444
_LOGGER.debug(f"Sunspec device unique id: {uid}")
4545
await self.async_set_unique_id(uid)
4646

4747
self._abort_if_unique_id_configured(
48-
updates={CONF_HOST: host, CONF_PORT: port, CONF_SLAVE_ID: slave_id}
48+
updates={CONF_HOST: host, CONF_PORT: port, CONF_UNIT_ID: unit_id}
4949
)
5050
self.init_info = user_input
5151
return await self.async_step_settings()
@@ -66,10 +66,10 @@ async def async_step_settings(self, user_input=None):
6666
self.init_info[CONF_SCAN_INTERVAL] = user_input[CONF_SCAN_INTERVAL]
6767
host = self.init_info[CONF_HOST]
6868
port = self.init_info[CONF_PORT]
69-
slave_id = self.init_info[CONF_SLAVE_ID]
69+
unit_id = self.init_info[CONF_UNIT_ID]
7070
_LOGGER.debug("Creating entry with data %s", self.init_info)
7171
return self.async_create_entry(
72-
title=f"{host}:{port}:{slave_id}", data=self.init_info
72+
title=f"{host}:{port}:{unit_id}", data=self.init_info
7373
)
7474

7575
return await self._show_settings_form(user_input)
@@ -81,14 +81,14 @@ def async_get_options_flow(config_entry):
8181

8282
async def _show_config_form(self, user_input): # pylint: disable=unused-argument
8383
"""Show the configuration form to edit connection data."""
84-
defaults = user_input or {CONF_HOST: "", CONF_PORT: 502, CONF_SLAVE_ID: 1}
84+
defaults = user_input or {CONF_HOST: "", CONF_PORT: 502, CONF_UNIT_ID: 1}
8585
return self.async_show_form(
8686
step_id="user",
8787
data_schema=vol.Schema(
8888
{
8989
vol.Required(CONF_HOST, default=defaults[CONF_HOST]): str,
9090
vol.Required(CONF_PORT, default=defaults[CONF_PORT]): int,
91-
vol.Required(CONF_SLAVE_ID, default=defaults[CONF_SLAVE_ID]): int,
91+
vol.Required(CONF_UNIT_ID, default=defaults[CONF_UNIT_ID]): int,
9292
}
9393
),
9494
errors=self._errors,
@@ -116,17 +116,17 @@ async def _show_settings_form(self, user_input): # pylint: disable=unused-argum
116116
errors=self._errors,
117117
)
118118

119-
async def _test_connection(self, host, port, slave_id):
119+
async def _test_connection(self, host, port, unit_id):
120120
"""Return true if credentials is valid."""
121-
_LOGGER.debug(f"Test connection to {host}:{port} slave id {slave_id}")
121+
_LOGGER.debug(f"Test connection to {host}:{port} unit id {unit_id}")
122122
try:
123-
self.client = SunSpecApiClient(host, port, slave_id, self.hass)
123+
self.client = SunSpecApiClient(host, port, unit_id, self.hass)
124124
self._device_info = await self.client.async_get_device_info()
125125
_LOGGER.info(self._device_info)
126126
return True
127127
except Exception as e: # pylint: disable=broad-except
128128
_LOGGER.error(
129-
"Failed to connect to host %s:%s slave %s - %s", host, port, slave_id, e
129+
"Failed to connect to host %s:%s unit %s - %s", host, port, unit_id, e
130130
)
131131
pass
132132
return False
@@ -162,15 +162,15 @@ async def show_settings_form(self, data=None, errors=None):
162162
settings = data or self.config_entry.data
163163
host = settings.get(CONF_HOST)
164164
port = settings.get(CONF_PORT)
165-
slave_id = settings.get(CONF_SLAVE_ID)
165+
unit_id = settings.get(CONF_UNIT_ID)
166166

167167
return self.async_show_form(
168168
step_id="host_options",
169169
data_schema=vol.Schema(
170170
{
171171
vol.Required(CONF_HOST, default=host): str,
172172
vol.Required(CONF_PORT, default=port): int,
173-
vol.Required(CONF_SLAVE_ID, default=slave_id): int,
173+
vol.Required(CONF_UNIT_ID, default=unit_id): int,
174174
}
175175
),
176176
errors=errors,
@@ -213,10 +213,10 @@ async def async_step_model_options(self, user_input=None):
213213
)
214214
except Exception as e: # pylint: disable=broad-except
215215
_LOGGER.error(
216-
"Failed to connect to host %s:%s slave %s - %s",
216+
"Failed to connect to host %s:%s unit %s - %s",
217217
self.settings[CONF_HOST],
218218
self.settings[CONF_PORT],
219-
self.settings[CONF_SLAVE_ID],
219+
self.settings[CONF_UNIT_ID],
220220
e,
221221
)
222222
return await self.show_settings_form(
@@ -227,7 +227,7 @@ async def _update_options(self):
227227
"""Update config entry options."""
228228
# self.settings[CONF_PORT] = 503
229229
# self.settings[CONF_ENABLED_MODELS] = [160, 103]
230-
title = f"{self.settings[CONF_HOST]}:{self.settings[CONF_PORT]}:{self.settings[CONF_SLAVE_ID]}"
230+
title = f"{self.settings[CONF_HOST]}:{self.settings[CONF_PORT]}:{self.settings[CONF_UNIT_ID]}"
231231
_LOGGER.debug(
232232
"Saving config entry with title %s, data: %s options %s",
233233
title,

custom_components/sunspec/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
CONF_ENABLED = "enabled"
2525
CONF_HOST = "host"
2626
CONF_PORT = "port"
27-
CONF_SLAVE_ID = "slave_id"
27+
CONF_UNIT_ID = "unit_id"
2828
CONF_PREFIX = "prefix"
2929
CONF_SCAN_INTERVAL = "scan_interval"
3030
CONF_ENABLED_MODELS = "models_enabled"

custom_components/sunspec/translations/en.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"data": {
88
"host": "Hostname/IP",
99
"port": "Port",
10-
"slave_id": "Slave ID"
10+
"unit_id": "Unit ID"
1111
}
1212
},
1313
"settings": {
@@ -35,7 +35,7 @@
3535
"data": {
3636
"host": "Hostname/IP",
3737
"port": "Port",
38-
"slave_id": "Slave ID"
38+
"unit_id": "Unit ID"
3939
}
4040
},
4141
"model_options": {
@@ -44,7 +44,7 @@
4444
"data": {
4545
"host": "Hostname/IP",
4646
"port": "Port",
47-
"slave_id": "Slave ID",
47+
"unit_id": "Unit ID",
4848
"models_enabled": "Read models",
4949
"scan_interval": "Scan interval (seconds)"
5050
}

custom_components/sunspec/translations/pl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"data": {
88
"host": "Nazwa hosta/Adres IP",
99
"port": "Port",
10-
"slave_id": "Identyfikator urządzenia"
10+
"unit_id": "Identyfikator urządzenia"
1111
}
1212
},
1313
"settings": {

custom_components/sunspec/translations/sk.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"data": {
88
"host": "Meno hosťa/IP",
99
"port": "Port",
10-
"slave_id": "Slave ID"
10+
"unit_id": "Unit ID"
1111
}
1212
},
1313
"settings": {
@@ -35,7 +35,7 @@
3535
"data": {
3636
"host": "Meno hosťa/IP",
3737
"port": "Port",
38-
"slave_id": "Slave ID"
38+
"unit_id": "Unit ID"
3939
}
4040
},
4141
"model_options": {
@@ -44,7 +44,7 @@
4444
"data": {
4545
"host": "Meno hosťa/IP",
4646
"port": "Port",
47-
"slave_id": "Slave ID",
47+
"unit_id": "Unit ID",
4848
"models_enabled": "Čítať modely",
4949
"scan_interval": "Interval skenovania (sekundy)"
5050
}

0 commit comments

Comments
 (0)