|
43 | 43 |
|
44 | 44 | DEVICE_CLASS_APPARENT_POWER = "apparent_power"
|
45 | 45 | DEVICE_CLASS_BATTERY = "battery"
|
| 46 | +DEVICE_CLASS_BUTTON = "button" |
46 | 47 | DEVICE_CLASS_CONNECTIVITY = "connectivity"
|
47 | 48 | DEVICE_CLASS_CURRENT = "current"
|
48 | 49 | DEVICE_CLASS_ENERGY = "energy"
|
|
87 | 88 | KEY_ENABLED_BY_DEFAULT = "en"
|
88 | 89 | KEY_ENTITY_CATEGORY = "ent_cat"
|
89 | 90 | KEY_ENTITY_PICTURE = "ent_pic"
|
| 91 | +KEY_EVENT_TYPES = "evt_typ" |
90 | 92 | KEY_EXPIRE_AFTER = "expire_after"
|
91 | 93 | KEY_HW_VERSION = "hw"
|
92 | 94 | KEY_ICON = "icon"
|
|
223 | 225 | TOPIC_COVER = "~status/cover:{cover}"
|
224 | 226 | TOPIC_EMDATA = "~status/emdata:{emeter_id}"
|
225 | 227 | TOPIC_EMETER = "~status/em:{emeter_id}"
|
| 228 | +TOPIC_EVENTS_RPC = "~events/rpc" |
226 | 229 | TOPIC_HUMIDITY = "~status/humidity:{sensor}"
|
227 | 230 | TOPIC_INPUT = "~status/input:{relay}"
|
228 | 231 | TOPIC_LIGHT = "~status/light:{light}"
|
|
260 | 263 | TPL_EMETER_TOTAL_APPARENT_POWER = "{{value_json.total_aprt_power}}"
|
261 | 264 | TPL_EMETER_TOTAL_CURRENT = "{{value_json.total_current}}"
|
262 | 265 | TPL_EMETER_VOLTAGE = "{{{{value_json.{phase}_voltage}}}}"
|
| 266 | +TPL_EVENT = "{{{^event_type^:value_json.params.events.0.event}|to_json}}" |
263 | 267 | TPL_FREQUENCY = "{{value_json.freq}}"
|
264 | 268 | TPL_ENERGY = "{{value_json.aenergy.total}}"
|
265 | 269 | TPL_ETH_IP = "{{value_json.eth.ip}}"
|
@@ -2052,6 +2056,36 @@ def get_input(input_id, input_type, event):
|
2052 | 2056 | return topic, payload
|
2053 | 2057 |
|
2054 | 2058 |
|
| 2059 | +def get_event(input_id, input_type): |
| 2060 | + """Create configuration for Shelly event entity.""" |
| 2061 | + topic = encode_config_topic( |
| 2062 | + f"{disc_prefix}/event/{device_id}-input-{input_id}/config" |
| 2063 | + ) |
| 2064 | + |
| 2065 | + if input_type != ATTR_BUTTON: |
| 2066 | + payload = "" |
| 2067 | + return topic, payload |
| 2068 | + |
| 2069 | + input_name = ( |
| 2070 | + device_config[f"input:{input_id}"].get(ATTR_NAME) or f"Button {input_id}" |
| 2071 | + ) |
| 2072 | + |
| 2073 | + payload = { |
| 2074 | + KEY_NAME: input_name, |
| 2075 | + KEY_STATE_TOPIC: TOPIC_EVENTS_RPC, |
| 2076 | + KEY_EVENT_TYPES: input_events, |
| 2077 | + KEY_VALUE_TEMPLATE: TPL_EVENT, |
| 2078 | + KEY_UNIQUE_ID: f"{device_id}-{input_id}".lower(), |
| 2079 | + KEY_QOS: qos, |
| 2080 | + KEY_AVAILABILITY: availability, |
| 2081 | + KEY_DEVICE: device_info, |
| 2082 | + KEY_DEFAULT_TOPIC: default_topic, |
| 2083 | + KEY_DEVICE_CLASS: DEVICE_CLASS_BUTTON, |
| 2084 | + } |
| 2085 | + |
| 2086 | + return topic, payload |
| 2087 | + |
| 2088 | + |
2055 | 2089 | def get_button(button, description):
|
2056 | 2090 | """Create configuration for Shelly button entity."""
|
2057 | 2091 | topic = encode_config_topic(f"{disc_prefix}/button/{device_id}-{button}/config")
|
@@ -2181,6 +2215,9 @@ def configure_device():
|
2181 | 2215 | for input_id in range(inputs):
|
2182 | 2216 | input_type = device_config[f"input:{input_id}"]["type"]
|
2183 | 2217 |
|
| 2218 | + topic, payload = get_event(input_id, input_type) |
| 2219 | + config[topic] = payload |
| 2220 | + |
2184 | 2221 | for event in input_events:
|
2185 | 2222 | topic, payload = get_input(input_id, input_type, event)
|
2186 | 2223 | config[topic] = payload
|
|
0 commit comments