|
| 1 | +# python |
| 2 | +# |
| 3 | +# This file is part of the mqttDisplayClient distribution: |
| 4 | +# (https://github.com/olialb/mqttDisplayClient). |
| 5 | +# Copyright (c) 2025 Oliver Albold. |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, version 3. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, but |
| 12 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +"""Module implements a class for home assistant discovery content |
| 20 | +in mqtt topics |
| 21 | +""" |
| 22 | + |
| 23 | +import uuid |
| 24 | +import json |
| 25 | + |
| 26 | +# |
| 27 | +# this file defines everthing whats needed to publish |
| 28 | +# the topics for homeassitant device discovery |
| 29 | +# |
| 30 | + |
| 31 | + |
| 32 | +class HADiscovery: |
| 33 | + """Implements methods to create content for home assitant |
| 34 | + auto discovery mqtt topics""" |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + device_name="MyDevice", |
| 39 | + base="homeassitant", |
| 40 | + manufacturer="MyCompany", |
| 41 | + model="MyModel", |
| 42 | + ): |
| 43 | + """Create class default values""" |
| 44 | + self.uid = "_" + str(hex(uuid.getnode())).replace("0x", "") + "_" |
| 45 | + self.device_name = device_name |
| 46 | + self.base = base |
| 47 | + self.manufacturer = manufacturer |
| 48 | + self.model = model |
| 49 | + |
| 50 | + def device(self): |
| 51 | + """json content of a device""" |
| 52 | + js = {} |
| 53 | + js["name"] = self.device_name |
| 54 | + js["identifiers"] = self.device_name + "_" + self.uid |
| 55 | + js["manufacturer"] = self.manufacturer |
| 56 | + js["model"] = self.model |
| 57 | + return js |
| 58 | + |
| 59 | + def sensor( # pylint: disable=too-many-arguments, too-many-positional-arguments |
| 60 | + self, |
| 61 | + name, |
| 62 | + state_topic, |
| 63 | + value_template=None, |
| 64 | + device_class=None, |
| 65 | + unit=None, |
| 66 | + icon=None |
| 67 | + ): |
| 68 | + """json content of a sensor""" |
| 69 | + uid = self.uid |
| 70 | + topic = self.base + "/sensor/" + uid + "/" + name.replace(" ", "_") + "/config" |
| 71 | + js = {} |
| 72 | + js["name"] = name |
| 73 | + js["unique_id"] = self.uid + "_" + name.replace(" ", "_") |
| 74 | + js["state_topic"] = state_topic |
| 75 | + if unit is not None: |
| 76 | + js["unit_of_measurement"] = unit |
| 77 | + if value_template is not None: |
| 78 | + js["value_template"] = "{{ value_json." + value_template + " }}" |
| 79 | + if device_class is not None: |
| 80 | + js["device_class"] = device_class |
| 81 | + if icon is not None: |
| 82 | + js['icon'] = "mdi:"+icon |
| 83 | + js["device"] = self.device() |
| 84 | + return topic, json.dumps(js) |
| 85 | + |
| 86 | + def switch(self, name, state_topic, value_template=None): |
| 87 | + """json content of a switch""" |
| 88 | + uid = self.uid |
| 89 | + topic = self.base + "/switch/" + uid + "/" + name.replace(" ", "_") + "/config" |
| 90 | + js = {} |
| 91 | + js["name"] = name |
| 92 | + js["unique_id"] = self.uid + "_" + name.replace(" ", "_") |
| 93 | + js["command_topic"] = state_topic + "/set" |
| 94 | + js["state_topic"] = state_topic |
| 95 | + js["payload_on"] = "ON" |
| 96 | + js["payload_off"] = "OFF" |
| 97 | + js["state_on"] = "ON" |
| 98 | + js["state_off"] = "OFF" |
| 99 | + if value_template is not None: |
| 100 | + js["value_template"] = "{{ value_json." + value_template + " }}" |
| 101 | + js["device"] = self.device() |
| 102 | + return topic, json.dumps(js) |
| 103 | + |
| 104 | + def text(self, name, state_topic, value_template=None): |
| 105 | + """json content of a text entity""" |
| 106 | + uid = self.uid |
| 107 | + topic = self.base + "/text/" + uid + "/" + name.replace(" ", "_") + "/config" |
| 108 | + js = {} |
| 109 | + js["name"] = name |
| 110 | + js["unique_id"] = self.uid + "_" + name.replace(" ", "_") |
| 111 | + js["command_topic"] = state_topic + "/set" |
| 112 | + js["state_topic"] = state_topic |
| 113 | + if value_template is not None: |
| 114 | + js["value_template"] = "{{ value_json." + value_template + " }}" |
| 115 | + js["device"] = self.device() |
| 116 | + return topic, json.dumps(js) |
| 117 | + |
| 118 | + def select(self, name, state_topic, options, value_template=None): |
| 119 | + """json content of a select entity""" |
| 120 | + uid = self.uid |
| 121 | + topic = self.base + "/select/" + uid + "/" + name.replace(" ", "_") + "/config" |
| 122 | + js = {} |
| 123 | + js["name"] = name |
| 124 | + js["unique_id"] = self.uid + "_" + name.replace(" ", "_") |
| 125 | + js["command_topic"] = state_topic + "/set" |
| 126 | + js["state_topic"] = state_topic |
| 127 | + js["options"] = options |
| 128 | + if value_template is not None: |
| 129 | + js["value_template"] = "{{ value_json." + value_template + " }}" |
| 130 | + js["device"] = self.device() |
| 131 | + return topic, json.dumps(js) |
| 132 | + |
| 133 | + def light( # pylint: disable=too-many-arguments, too-many-positional-arguments |
| 134 | + self, |
| 135 | + name, |
| 136 | + state_topic, |
| 137 | + brightness_topic, |
| 138 | + value_template_state=None, |
| 139 | + value_tmpl_brightness=None, |
| 140 | + brightness_scale=100, |
| 141 | + ): |
| 142 | + """json content of a light""" |
| 143 | + uid = self.uid |
| 144 | + topic = self.base + "/light/" + uid + "/" + name.replace(" ", "_") + "/config" |
| 145 | + js = {} |
| 146 | + js["name"] = name |
| 147 | + js["unique_id"] = self.uid + "_" + name.replace(" ", "_") |
| 148 | + js["command_topic"] = state_topic + "/set" |
| 149 | + js["state_topic"] = state_topic |
| 150 | + js["payload_on"] = "ON" |
| 151 | + js["payload_off"] = "OFF" |
| 152 | + js["state_on"] = "ON" |
| 153 | + js["state_off"] = "OFF" |
| 154 | + js["brightness_scale"] = brightness_scale |
| 155 | + js["brightness_command_topic"] = brightness_topic + "/set" |
| 156 | + js["brightness_state_topic"] = brightness_topic |
| 157 | + if value_template_state is not None: |
| 158 | + js["state_value_template"] = "{{ value_json." + value_template_state + " }}" |
| 159 | + if value_tmpl_brightness is not None: |
| 160 | + js["brightness_value_template"] = ( |
| 161 | + "{{ value_json." + value_tmpl_brightness + " }}" |
| 162 | + ) |
| 163 | + js["device"] = self.device() |
| 164 | + return topic, json.dumps(js) |
0 commit comments