Skip to content

Commit e7c5751

Browse files
authored
Implement event platform (#245)
* Bump min HA version * Update readme files * Update readme files * Add event entities for button inputs
1 parent cff525b commit e7c5751

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ logger:
7878
7979
- binary sensor
8080
- button
81+
- cover
82+
- device trigger
83+
- event
8184
- fan
8285
- light
8386
- sensor

hacs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Shellies Discovery Gen2",
3-
"homeassistant": "2023.3.0",
3+
"homeassistant": "2023.8.0",
44
"zip_release": true,
55
"filename": "shellies-discovery-gen2.zip"
66
}

info.md

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ logger:
7171
7272
- binary sensor
7373
- button
74+
- cover
75+
- device trigger
76+
- event
7477
- fan
7578
- light
7679
- sensor

python_scripts/shellies_discovery_gen2.py

+37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
DEVICE_CLASS_APPARENT_POWER = "apparent_power"
4545
DEVICE_CLASS_BATTERY = "battery"
46+
DEVICE_CLASS_BUTTON = "button"
4647
DEVICE_CLASS_CONNECTIVITY = "connectivity"
4748
DEVICE_CLASS_CURRENT = "current"
4849
DEVICE_CLASS_ENERGY = "energy"
@@ -87,6 +88,7 @@
8788
KEY_ENABLED_BY_DEFAULT = "en"
8889
KEY_ENTITY_CATEGORY = "ent_cat"
8990
KEY_ENTITY_PICTURE = "ent_pic"
91+
KEY_EVENT_TYPES = "evt_typ"
9092
KEY_EXPIRE_AFTER = "expire_after"
9193
KEY_HW_VERSION = "hw"
9294
KEY_ICON = "icon"
@@ -223,6 +225,7 @@
223225
TOPIC_COVER = "~status/cover:{cover}"
224226
TOPIC_EMDATA = "~status/emdata:{emeter_id}"
225227
TOPIC_EMETER = "~status/em:{emeter_id}"
228+
TOPIC_EVENTS_RPC = "~events/rpc"
226229
TOPIC_HUMIDITY = "~status/humidity:{sensor}"
227230
TOPIC_INPUT = "~status/input:{relay}"
228231
TOPIC_LIGHT = "~status/light:{light}"
@@ -260,6 +263,7 @@
260263
TPL_EMETER_TOTAL_APPARENT_POWER = "{{value_json.total_aprt_power}}"
261264
TPL_EMETER_TOTAL_CURRENT = "{{value_json.total_current}}"
262265
TPL_EMETER_VOLTAGE = "{{{{value_json.{phase}_voltage}}}}"
266+
TPL_EVENT = "{{{^event_type^:value_json.params.events.0.event}|to_json}}"
263267
TPL_FREQUENCY = "{{value_json.freq}}"
264268
TPL_ENERGY = "{{value_json.aenergy.total}}"
265269
TPL_ETH_IP = "{{value_json.eth.ip}}"
@@ -2052,6 +2056,36 @@ def get_input(input_id, input_type, event):
20522056
return topic, payload
20532057

20542058

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+
20552089
def get_button(button, description):
20562090
"""Create configuration for Shelly button entity."""
20572091
topic = encode_config_topic(f"{disc_prefix}/button/{device_id}-{button}/config")
@@ -2181,6 +2215,9 @@ def configure_device():
21812215
for input_id in range(inputs):
21822216
input_type = device_config[f"input:{input_id}"]["type"]
21832217

2218+
topic, payload = get_event(input_id, input_type)
2219+
config[topic] = payload
2220+
21842221
for event in input_events:
21852222
topic, payload = get_input(input_id, input_type, event)
21862223
config[topic] = payload

0 commit comments

Comments
 (0)