Add device triggers to support setting turn_on event (#35456)
* Add device triggers to support setting turn_on event * Add turn on event * Add unique_id based on config entry * Adjust tests for addition of uuid * Supported features are now always same * Switch to player_setup fixture that actually start platform * Update homeassistant/components/arcam_fmj/const.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
37cabe5562
commit
5c0d237c2d
8 files changed
+241
-38
No files matched your search
@@ -5,6 +5,8 @@ SIGNAL_CLIENT_STARTED = "arcam.client_started"
|
||||
SIGNAL_CLIENT_STOPPED = "arcam.client_stopped"
|
||||
SIGNAL_CLIENT_DATA = "arcam.client_data"
|
||||
|
||||
EVENT_TURN_ON = "arcam_fmj.turn_on"
|
||||
|
||||
DEFAULT_PORT = 50000
|
||||
DEFAULT_NAME = "Arcam FMJ"
|
||||
DEFAULT_SCAN_INTERVAL = 5
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Provides device automations for Arcam FMJ Receiver control."""
|
||||
from typing import List
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.automation import AutomationActionType
|
||||
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_PLATFORM,
|
||||
CONF_TYPE,
|
||||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv, entity_registry
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN, EVENT_TURN_ON
|
||||
|
||||
TRIGGER_TYPES = {"turn_on"}
|
||||
TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant, device_id: str) -> List[dict]:
|
||||
"""List device triggers for Arcam FMJ Receiver control devices."""
|
||||
registry = await entity_registry.async_get_registry(hass)
|
||||
triggers = []
|
||||
|
||||
# Get all the integrations entities for this device
|
||||
for entry in entity_registry.async_entries_for_device(registry, device_id):
|
||||
if entry.domain == "media_player":
|
||||
triggers.append(
|
||||
{
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DEVICE_ID: device_id,
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_ENTITY_ID: entry.entity_id,
|
||||
CONF_TYPE: "turn_on",
|
||||
}
|
||||
)
|
||||
|
||||
return triggers
|
||||
|
||||
|
||||
async def async_attach_trigger(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: AutomationActionType,
|
||||
automation_info: dict,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Attach a trigger."""
|
||||
config = TRIGGER_SCHEMA(config)
|
||||
|
||||
if config[CONF_TYPE] == "turn_on":
|
||||
|
||||
@callback
|
||||
def _handle_event(event: Event):
|
||||
if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]:
|
||||
hass.async_run_job(action({"trigger": config}, context=event.context))
|
||||
|
||||
return hass.bus.async_listen(EVENT_TURN_ON, _handle_event)
|
||||
|
||||
return lambda: None
|
||||
@@ -18,6 +18,7 @@ from homeassistant.components.media_player.const import (
|
||||
SUPPORT_VOLUME_STEP,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
CONF_ZONE,
|
||||
SERVICE_TURN_ON,
|
||||
@@ -31,6 +32,7 @@ from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
DOMAIN_DATA_ENTRIES,
|
||||
EVENT_TURN_ON,
|
||||
SIGNAL_CLIENT_DATA,
|
||||
SIGNAL_CLIENT_STARTED,
|
||||
SIGNAL_CLIENT_STOPPED,
|
||||
@@ -53,6 +55,7 @@ async def async_setup_entry(
|
||||
[
|
||||
ArcamFmj(
|
||||
State(client, zone),
|
||||
config_entry.unique_id or config_entry.entry_id,
|
||||
zone_config[CONF_NAME],
|
||||
zone_config.get(SERVICE_TURN_ON),
|
||||
)
|
||||
@@ -67,9 +70,12 @@ async def async_setup_entry(
|
||||
class ArcamFmj(MediaPlayerEntity):
|
||||
"""Representation of a media device."""
|
||||
|
||||
def __init__(self, state: State, name: str, turn_on: Optional[ConfigType]):
|
||||
def __init__(
|
||||
self, state: State, uuid: str, name: str, turn_on: Optional[ConfigType]
|
||||
):
|
||||
"""Initialize device."""
|
||||
self._state = state
|
||||
self._uuid = uuid
|
||||
self._name = name
|
||||
self._turn_on = turn_on
|
||||
self._support = (
|
||||
@@ -78,6 +84,7 @@ class ArcamFmj(MediaPlayerEntity):
|
||||
| SUPPORT_VOLUME_MUTE
|
||||
| SUPPORT_VOLUME_STEP
|
||||
| SUPPORT_TURN_OFF
|
||||
| SUPPORT_TURN_ON
|
||||
)
|
||||
if state.zn == 1:
|
||||
self._support |= SUPPORT_SELECT_SOUND_MODE
|
||||
@@ -95,6 +102,11 @@ class ArcamFmj(MediaPlayerEntity):
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique identifier if known."""
|
||||
return f"{self._uuid}-{self._state.zn}"
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return a device description for device registry."""
|
||||
@@ -124,10 +136,7 @@ class ArcamFmj(MediaPlayerEntity):
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag media player features that are supported."""
|
||||
support = self._support
|
||||
if self._state.get_power() is not None or self._turn_on:
|
||||
support |= SUPPORT_TURN_ON
|
||||
return support
|
||||
return self._support
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Once registered, add listener for events."""
|
||||
@@ -230,7 +239,8 @@ class ArcamFmj(MediaPlayerEntity):
|
||||
validate_config=False,
|
||||
)
|
||||
else:
|
||||
_LOGGER.error("Unable to turn on")
|
||||
_LOGGER.debug("Firing event to turn on device")
|
||||
self.hass.bus.async_fire(EVENT_TURN_ON, {ATTR_ENTITY_ID: self.entity_id})
|
||||
|
||||
async def async_turn_off(self):
|
||||
"""Turn the media player off."""
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"device_automation": {
|
||||
"trigger_type": {
|
||||
"turn_on": "{entity_name} was requested to turn on"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
{
|
||||
"title": "Arcam FMJ"
|
||||
"title": "Arcam FMJ",
|
||||
"device_automation": {
|
||||
"trigger_type": {
|
||||
"turn_on": "{entity_name} was requested to turn on"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user