Skip to content

Commit

Permalink
user-defined unique_id, for certain very-specific scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarcelo committed Feb 27, 2023
1 parent 2d089c3 commit 97c8685
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ham/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.5.3"
__version__ = "0.5.4"

from .manager import MqttManager

Expand Down
21 changes: 16 additions & 5 deletions src/ham/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class MqttManager(Thread):
node_id: str
base_topic: str
name: str
unique_identifier: str

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

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

self.things = defaultdict(list)
self.unique_identifier = unique_identifier or self.get_mac()
self.device_info = self._gen_device_info()

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

# New dictionary with sensible defaults
config = common_config.copy()
config["unique_id"] = f"{ self.get_mac() }_{ thing.short_id }"
config["unique_id"] = f"{ self.unique_identifier }_{ thing.short_id }"

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

def _gen_device_info(self) -> DeviceInfo:
"""Generate the device information payload."""
mac_address = self.get_mac()
return {
"name": self.name,
"identifiers": [f"{self.name}_{mac_address}"],
"connections": [("mac", mac_address)],
"identifiers": [f"{self.name}_{self.unique_identifier}"],
"connections": [("mac", self.get_mac())],
"sw_version": __version__,
}

0 comments on commit 97c8685

Please sign in to comment.