|
3 | 3 | import ipaddress
|
4 | 4 | import struct
|
5 | 5 |
|
6 |
| -from homeassistant.exceptions import HomeAssistantError |
7 |
| - |
8 | 6 | from .const import DOMAIN_REGEX
|
9 | 7 |
|
10 | 8 |
|
@@ -45,72 +43,3 @@ def host_valid(host):
|
45 | 43 |
|
46 | 44 | except ValueError:
|
47 | 45 | return DOMAIN_REGEX.match(host)
|
48 |
| - |
49 |
| - |
50 |
| -def deviceIdsFromString(value: str) -> list[int]: |
51 |
| - """The function `deviceIdsFromString` takes a string input and returns a list of |
52 |
| - device IDs, where the input can be a single ID or a range of IDs separated by commas |
53 |
| -
|
54 |
| - Parameters |
55 |
| - ---------- |
56 |
| - value |
57 |
| - The `value` parameter is a string that represents a list of device IDs. The |
58 |
| - device IDs can be specified as individual IDs or as ranges separated by a hyphen |
59 |
| - For example, the string "1,3-5,7" represents the device IDs 1, 3, 4, 5 and 7 |
60 |
| -
|
61 |
| - Returns |
62 |
| - ------- |
63 |
| - The function `checkDeviceIds` returns a list of device IDs. |
64 |
| -
|
65 |
| - Credit: https://github.com/thargy/modbus-scanner/blob/main/scan.py |
66 |
| - """ |
67 |
| - parts = [p.strip() for p in value.split(",")] |
68 |
| - ids = [] |
69 |
| - for p in parts: |
70 |
| - r = [i.strip() for i in p.split("-")] |
71 |
| - if len(r) < 2: |
72 |
| - # We have a single id |
73 |
| - ids.append(checkDeviceId(r[0])) |
74 |
| - |
75 |
| - elif len(r) > 2: |
76 |
| - # Invalid range, multiple '-'s |
77 |
| - raise HomeAssistantError( |
78 |
| - f"'{p}' in '{value}' looks like a range but has multiple '-'s." |
79 |
| - ) |
80 |
| - |
81 |
| - else: |
82 |
| - # Looks like a range |
83 |
| - start = checkDeviceId(r[0]) |
84 |
| - end = checkDeviceId(r[1]) |
85 |
| - if end < start: |
86 |
| - raise HomeAssistantError( |
87 |
| - f"'{start}' must be less than or equal to {end}." |
88 |
| - ) |
89 |
| - |
90 |
| - ids.extend(range(start, end + 1)) |
91 |
| - |
92 |
| - return sorted(set(ids)) |
93 |
| - |
94 |
| - |
95 |
| -def checkDeviceId(value: str | int) -> int: |
96 |
| - """The `checkDeviceId` function takes a value and checks if it is a valid device |
97 |
| - ID between 1 and 247, raising an error if it is not. |
98 |
| -
|
99 |
| - Parameters |
100 |
| - ---------- |
101 |
| - value |
102 |
| - The value parameter is the input value that is |
103 |
| - being checked for validity as a device ID. |
104 |
| -
|
105 |
| - Returns |
106 |
| - ------- |
107 |
| - the device ID as an integer. |
108 |
| -
|
109 |
| - Credit: https://github.com/thargy/modbus-scanner/blob/main/scan.py |
110 |
| - """ |
111 |
| - id = int(value) |
112 |
| - |
113 |
| - if (id < 1) or id > 247: |
114 |
| - raise HomeAssistantError(f"'{value}' must be a device ID between 1 and 247") |
115 |
| - |
116 |
| - return id |
0 commit comments