Skip to content

Commit

Permalink
Try to convert in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed May 24, 2023
1 parent af105a7 commit dff73a2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 146 deletions.
22 changes: 8 additions & 14 deletions homeassistant/components/samsungtv/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
async_get_client_by_device_entry,
async_get_device_entry_by_device_id,
)
from .triggers.turn_off import (
PLATFORM_TYPE as TURN_OFF_PLATFORM_TYPE,
async_get_turn_off_trigger,
)
from .triggers.turn_on import (
PLATFORM_TYPE as TURN_ON_PLATFORM_TYPE,
async_get_turn_on_trigger,
from .triggers.turn_on_off import (
PLATFORM_TYPE_TURN_OFF,
PLATFORM_TYPE_TURN_ON,
async_get_turn_on_off_triggers,
)

TRIGGER_TYPES = {TURN_ON_PLATFORM_TYPE, TURN_OFF_PLATFORM_TYPE}
TRIGGER_TYPES = {PLATFORM_TYPE_TURN_ON, PLATFORM_TYPE_TURN_OFF}
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
Expand All @@ -42,7 +39,7 @@ async def async_validate_trigger_config(
"""Validate config."""
config = TRIGGER_SCHEMA(config)

if config[CONF_TYPE] in [TURN_ON_PLATFORM_TYPE, TURN_OFF_PLATFORM_TYPE]:
if config[CONF_TYPE] in [PLATFORM_TYPE_TURN_ON, PLATFORM_TYPE_TURN_OFF]:
device_id = config[CONF_DEVICE_ID]
try:
device = async_get_device_entry_by_device_id(hass, device_id)
Expand All @@ -58,10 +55,7 @@ async def async_get_triggers(
_hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device triggers for device."""
triggers = [
async_get_turn_on_trigger(device_id),
async_get_turn_off_trigger(device_id),
]
triggers = async_get_turn_on_off_triggers(device_id)
return triggers


Expand All @@ -73,7 +67,7 @@ async def async_attach_trigger(
) -> CALLBACK_TYPE:
"""Attach a trigger."""
trigger_type = config[CONF_TYPE]
if config[CONF_TYPE] in [TURN_ON_PLATFORM_TYPE, TURN_OFF_PLATFORM_TYPE]:
if config[CONF_TYPE] in [PLATFORM_TYPE_TURN_ON, PLATFORM_TYPE_TURN_OFF]:
trigger_config = {
CONF_PLATFORM: trigger_type,
CONF_DEVICE_ID: config[CONF_DEVICE_ID],
Expand Down
17 changes: 13 additions & 4 deletions homeassistant/components/samsungtv/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
DOMAIN,
LOGGER,
)
from .triggers.turn_off import async_get_turn_off_trigger
from .triggers.turn_on import async_get_turn_on_trigger
from .triggers.turn_on_off import (
PLATFORM_TYPE_TURN_OFF,
PLATFORM_TYPE_TURN_ON,
async_get_turn_on_off_triggers,
)

SOURCES = {"TV": "KEY_TV", "HDMI": "KEY_HDMI"}

Expand Down Expand Up @@ -374,12 +377,18 @@ async def async_added_to_hass(self) -> None:
if (entry := self.registry_entry) and entry.device_id:
self.async_on_remove(
self._turn_on.async_register(
self.hass, async_get_turn_on_trigger(entry.device_id)
self.hass,
async_get_turn_on_off_triggers(
entry.device_id, PLATFORM_TYPE_TURN_ON
)[0],
),
)
self.async_on_remove(
self._turn_off.async_register(
self.hass, async_get_turn_off_trigger(entry.device_id)
self.hass,
async_get_turn_on_off_triggers(
entry.device_id, PLATFORM_TYPE_TURN_OFF
)[0],
),
)

Expand Down
8 changes: 5 additions & 3 deletions homeassistant/components/samsungtv/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
)
from homeassistant.helpers.typing import ConfigType

from .triggers import turn_off, turn_on
from .triggers import turn_on_off

TRIGGERS = {
"turn_on": turn_on,
"turn_off": turn_off,
# Not sure what to do here
"turn_on": turn_on_off,
"turn_off": turn_on_off,
}


Expand All @@ -25,6 +26,7 @@ def _get_trigger_platform(config: ConfigType) -> TriggerProtocol:
platform_split = config[CONF_PLATFORM].split(".", maxsplit=1)
if len(platform_split) < 2 or platform_split[1] not in TRIGGERS:
raise ValueError(f"Unknown Samsung TV trigger platform {config[CONF_PLATFORM]}")
# Not sure what to do here too
return cast(TriggerProtocol, TRIGGERS[platform_split[1]])


Expand Down
108 changes: 0 additions & 108 deletions homeassistant/components/samsungtv/triggers/turn_on.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Samsung TV device turn off trigger."""
"""Samsung TV device turn on and turn off triggers."""
from __future__ import annotations

import voluptuous as vol
Expand Down Expand Up @@ -26,15 +26,18 @@
async_get_device_id_from_entity_id,
)

# Platform type should be <DOMAIN>.<SUBMODULE_NAME>
PLATFORM_TYPE = f"{DOMAIN}.{__name__.rsplit('.', maxsplit=1)[-1]}"

TRIGGER_TYPE_TURN_ON = "turn_on"
TRIGGER_TYPE_TURN_OFF = "turn_off"

PLATFORM_TYPE_TURN_ON = f"{DOMAIN}.{TRIGGER_TYPE_TURN_ON}"
PLATFORM_TYPE_TURN_OFF = f"{DOMAIN}.{TRIGGER_TYPE_TURN_OFF}"

TRIGGER_SCHEMA = vol.All(
cv.TRIGGER_BASE_SCHEMA.extend(
{
vol.Required(CONF_PLATFORM): PLATFORM_TYPE,
vol.Required(CONF_PLATFORM): vol.In(
PLATFORM_TYPE_TURN_ON, PLATFORM_TYPE_TURN_OFF
),
vol.Optional(ATTR_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
},
Expand All @@ -43,15 +46,27 @@
)


def async_get_turn_off_trigger(device_id: str) -> dict[str, str]:
"""Return data for a turn off trigger."""
def async_get_turn_on_off_triggers(
device_id: str, platform_type: str | None = None
) -> list[dict[str, str]]:
"""Return data for turn on and turn off triggers."""

if platform_type is None:
platforms = [PLATFORM_TYPE_TURN_ON, PLATFORM_TYPE_TURN_OFF]
else:
platforms = [platform_type]

return {
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: PLATFORM_TYPE,
}
triggers = []
for platform in platforms:
triggers.append(
{
CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: platform,
}
)
return triggers


async def async_attach_trigger(
Expand All @@ -60,7 +75,7 @@ async def async_attach_trigger(
action: TriggerActionType,
trigger_info: TriggerInfo,
*,
platform_type: str = PLATFORM_TYPE,
platform_type: str,
) -> CALLBACK_TYPE | None:
"""Attach a trigger."""
device_ids = set()
Expand All @@ -82,19 +97,21 @@ async def async_attach_trigger(
for device_id in device_ids:
device = async_get_device_entry_by_device_id(hass, device_id)
device_name = device.name_by_user or device.name
# Example: extracts "turn off" from "samsungtv.turn_off"
trigger_name = platform_type.split(".")[1].replace("_", " ")

variables = {
**trigger_data,
CONF_PLATFORM: platform_type,
ATTR_DEVICE_ID: device_id,
"description": f"Samsung turn off trigger for {device_name}",
"description": f"Samsung {trigger_name} trigger for {device_name}",
}

turn_off_trigger = async_get_turn_off_trigger(device_id)
trigger = async_get_turn_on_off_triggers(device_id, platform_type)[0]

unsubs.append(
PluggableAction.async_attach_trigger(
hass, turn_off_trigger, action, {"trigger": variables}
hass, trigger, action, {"trigger": variables}
)
)

Expand Down

0 comments on commit dff73a2

Please sign in to comment.