Skip to content

Commit 97c8685

Browse files
committed
user-defined unique_id, for certain very-specific scenarios
1 parent 2d089c3 commit 97c8685

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/ham/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.5.3"
1+
__version__ = "0.5.4"
22

33
from .manager import MqttManager
44

src/ham/manager.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class MqttManager(Thread):
3535
node_id: str
3636
base_topic: str
3737
name: str
38+
unique_identifier: str
3839

3940
def __init__(self, host='localhost', port=1883, username=None,
4041
password=None, *, node_id=None, base_topic=None,
41-
discovery_prefix='homeassistant', name=None):
42+
discovery_prefix='homeassistant', name=None,
43+
unique_identifier=None):
4244
"""Initialize connection to the MQTT the server.
4345
4446
This will prepare the MQTT connection using the provided configuration
@@ -49,6 +51,15 @@ def __init__(self, host='localhost', port=1883, username=None,
4951
If not specified, the hostname will be used for both `node_id` and
5052
`base_topic` while the default 'homeassistant' will be used as the
5153
`discovery_prefix`.
54+
55+
Setting `unique_identifier` is somewhat advanced. See:
56+
https://developers.home-assistant.io/docs/entity_registry_index
57+
58+
If you set the `unique_identifier`, it will be used for generating the
59+
unique_id for the Things. If not set, the mac of the device will be used
60+
(recommended). Set this ONLY if the MAC of the host is erratic (e.g. if
61+
you are using certain ARM single-board-computers that are MACless,
62+
or if you are deploying into kubernetes).
5263
"""
5364
super().__init__()
5465

@@ -90,6 +101,7 @@ def __init__(self, host='localhost', port=1883, username=None,
90101
self.name = self.node_id
91102

92103
self.things = defaultdict(list)
104+
self.unique_identifier = unique_identifier or self.get_mac()
93105
self.device_info = self._gen_device_info()
94106

95107
logger.debug("Initialization parameters: node_id=%s, base_topic=%s, discovery_prefix=%s, name=%s",
@@ -189,7 +201,7 @@ def on_connect(self, _, userdata, flags, rc):
189201

190202
# New dictionary with sensible defaults
191203
config = common_config.copy()
192-
config["unique_id"] = f"{ self.get_mac() }_{ thing.short_id }"
204+
config["unique_id"] = f"{ self.unique_identifier }_{ thing.short_id }"
193205

194206
# Then call get_config, and allow the implementation to override
195207
# the previously set defaults (at their own risk)
@@ -213,10 +225,9 @@ def on_connect(self, _, userdata, flags, rc):
213225

214226
def _gen_device_info(self) -> DeviceInfo:
215227
"""Generate the device information payload."""
216-
mac_address = self.get_mac()
217228
return {
218229
"name": self.name,
219-
"identifiers": [f"{self.name}_{mac_address}"],
220-
"connections": [("mac", mac_address)],
230+
"identifiers": [f"{self.name}_{self.unique_identifier}"],
231+
"connections": [("mac", self.get_mac())],
221232
"sw_version": __version__,
222233
}

0 commit comments

Comments
 (0)