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

Add Kodi keypress event #93321

Merged
merged 6 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion homeassistant/components/kodi/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import voluptuous as vol

from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.homeassistant.triggers import event as event_trigger
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_ID,
Expand All @@ -19,7 +20,7 @@

from .const import DOMAIN, EVENT_TURN_OFF, EVENT_TURN_ON

TRIGGER_TYPES = {"turn_on", "turn_off"}
TRIGGER_TYPES = {"turn_on", "turn_off", "keypress"}

TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{
Expand Down Expand Up @@ -57,6 +58,15 @@ async def async_get_triggers(
CONF_TYPE: "turn_off",
}
)
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_TYPE: "keypress",
}
)

return triggers

Expand Down Expand Up @@ -97,4 +107,19 @@ async def async_attach_trigger(
if config[CONF_TYPE] == "turn_off":
return _attach_trigger(hass, config, action, EVENT_TURN_OFF, trigger_info)

if config[CONF_TYPE] == "keypress":
event_config = event_trigger.TRIGGER_SCHEMA(
{
event_trigger.CONF_PLATFORM: "event",
event_trigger.CONF_EVENT_TYPE: f"{DOMAIN}_keypress",
event_trigger.CONF_EVENT_DATA: {
CONF_DEVICE_ID: config[CONF_DEVICE_ID],
CONF_TYPE: config[CONF_TYPE],
},
}
)
return await event_trigger.async_attach_trigger(
hass, event_config, action, trigger_info, platform_type="device"
)

return lambda: None
18 changes: 18 additions & 0 deletions homeassistant/components/kodi/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_ID,
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_PROXY_SSL,
CONF_SSL,
CONF_TIMEOUT,
CONF_TYPE,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STARTED,
)
Expand Down Expand Up @@ -279,6 +281,7 @@ def __init__(self, connection, kodi, name, uid):
self._connection = connection
self._kodi = kodi
self._unique_id = uid
self._device_id = None
self._players = None
self._properties = {}
self._item = {}
Expand Down Expand Up @@ -336,6 +339,19 @@ def async_on_volume_changed(self, sender, data):
self._app_properties["muted"] = data["muted"]
self.async_write_ha_state()

@callback
def async_on_key_press(self, sender, data):
"""Handle a incoming key press notification."""
self.hass.bus.async_fire(
f"{DOMAIN}_keypress",
{
CONF_TYPE: "keypress",
CONF_DEVICE_ID: self._device_id,
"sender": sender,
"data": data,
},
)

async def async_on_quit(self, sender, data):
"""Reset the player state on quit action."""
await self._clear_connection()
Expand Down Expand Up @@ -410,6 +426,7 @@ async def _on_ws_connected(self):
dev_reg = dr.async_get(self.hass)
device = dev_reg.async_get_device({(DOMAIN, self.unique_id)})
dev_reg.async_update_device(device.id, sw_version=sw_version)
self._device_id = device.id

self.async_schedule_update_ha_state(True)

Expand Down Expand Up @@ -457,6 +474,7 @@ def _register_ws_callbacks(self):
self._connection.server.Application.OnVolumeChanged = (
self.async_on_volume_changed
)
self._connection.server.Other.OnKeyPress = self.async_on_key_press
self._connection.server.System.OnQuit = self.async_on_quit
self._connection.server.System.OnRestart = self.async_on_quit
self._connection.server.System.OnSleep = self.async_on_quit
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/kodi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"device_automation": {
"trigger_type": {
"turn_on": "{entity_name} was requested to turn on",
"turn_off": "{entity_name} was requested to turn off"
"turn_off": "{entity_name} was requested to turn off",
"keypress": "Key press"
}
}
}
19 changes: 18 additions & 1 deletion tests/components/kodi/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def test_get_triggers(
"entity_id": f"{MP_DOMAIN}.kodi_5678",
"metadata": {"secondary": False},
}
for trigger in ["turn_off", "turn_on"]
for trigger in ["turn_off", "turn_on", "keypress"]
]

# Test triggers are either kodi specific triggers or media_player entity triggers
Expand Down Expand Up @@ -112,6 +112,23 @@ async def test_if_fires_on_state_change(
},
},
},
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": kodi_media_player,
"type": "keypress",
},
"action": {
"service": "test.automation",
"data_template": {
"some": (
"keypress - {{ trigger.entity_id }} - {{ trigger.id}}"
)
},
},
},
]
},
)
Expand Down