Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ [refactor] centralize the generation of the device payload #70

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 24 additions & 48 deletions apps/qolsysgw/mqtt/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ def payload_available(self):
def payload_unavailable(self):
return 'offline'

@property
def device_payload(self):
payload = {
'name': self._cfg.panel_device_name,
'identifiers': [
self._cfg.panel_unique_id,
],
'manufacturer': 'Qolsys',
'model': 'IQ Panel 2+',
}

# If we are able to resolve the mac address, this will allow to
# link the device to other related elements in home assistant
mac = self._cfg.panel_mac or get_mac_from_host(self._cfg.panel_host)
if mac:
payload['connections'] = [
['mac', mac],
]

return payload

@property
def configure_availability(self):
availability = [
Expand Down Expand Up @@ -249,22 +270,7 @@ def configure_payload(self, **kwargs):
}

payload['unique_id'] = f"{self._cfg.panel_unique_id}_last_error"
payload['device'] = {
'name': self._cfg.panel_device_name,
'identifiers': [
self._cfg.panel_unique_id,
],
'manufacturer': 'Qolsys',
'model': 'IQ Panel 2+',
}

# If we are able to resolve the mac address, this will allow to
# link the device to other related elements in home assistant
mac = self._cfg.panel_mac or get_mac_from_host(self._cfg.panel_host)
if mac:
payload['device']['connections'] = [
['mac', mac],
]
payload['device'] = self.device_payload

return payload

Expand Down Expand Up @@ -373,22 +379,7 @@ def configure_payload(self, **kwargs):
# together; this will also allow to interact with the partition in
# the UI, change it's name, assign it to areas, etc.
payload['unique_id'] = f"{self._cfg.panel_unique_id}_p{self._partition.id}"
payload['device'] = {
'name': self._cfg.panel_device_name,
'identifiers': [
self._cfg.panel_unique_id,
],
'manufacturer': 'Qolsys',
'model': 'IQ Panel 2+',
}

# If we are able to resolve the mac address, this will allow to
# link the device to other related elements in home assistant
mac = self._cfg.panel_mac or get_mac_from_host(self._cfg.panel_host)
if mac:
payload['device']['connections'] = [
['mac', mac],
]
payload['device'] = self.device_payload

if self._cfg.default_trigger_command:
payload['payload_trigger'] = self._cfg.default_trigger_command
Expand Down Expand Up @@ -497,22 +488,7 @@ def configure_payload(self, partition: QolsysPartition, **kwargs):
payload['unique_id'] = f"{self._cfg.panel_unique_id}_"\
f"p{self._sensor.partition_id}"\
f"z{self._sensor.zone_id}"
payload['device'] = {
'name': self._cfg.panel_device_name,
'identifiers': [
self._cfg.panel_unique_id,
],
'manufacturer': 'Qolsys',
'model': 'IQ Panel 2+',
}

# If we are able to resolve the mac address, this will allow to
# link the device to other related elements in home assistant
mac = self._cfg.panel_mac or get_mac_from_host(self._cfg.panel_host)
if mac:
payload['device']['connections'] = [
['mac', mac],
]
payload['device'] = self.device_payload

return payload

Expand Down