Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions custom_components/samsungtv_smart/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
__version__,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import config_validation as cv, entity_registry as er

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not import cv only for cv.string. Is enought use str

from homeassistant.helpers.selector import (
EntitySelector,
EntitySelectorConfig,
Expand All @@ -50,6 +50,7 @@
CONF_PING_PORT,
CONF_POWER_ON_DELAY,
CONF_POWER_ON_METHOD,
CONF_POWER_ON_SERVICE_DATA,
CONF_SHOW_CHANNEL_NR,
CONF_SYNC_TURN_OFF,
CONF_SYNC_TURN_ON,
Expand Down Expand Up @@ -100,6 +101,7 @@
POWER_ON_METHODS = {
PowerOnMethod.WOL.value: "WOL Packet (better for wired connection)",
PowerOnMethod.SmartThings.value: "SmartThings (better for wireless connection)",
PowerOnMethod.Service.value: "Service",
}

CONF_SHOW_ADV_OPT = "show_adv_opt"
Expand Down Expand Up @@ -484,6 +486,18 @@ def _async_option_form(self):
CONF_SYNC_TURN_ON,
description={"suggested_value": options.get(CONF_SYNC_TURN_ON, [])},
): EntitySelector(select_entities),
vol.Required(
CONF_POWER_ON_METHOD,
default=options.get(
CONF_POWER_ON_METHOD, str(PowerOnMethod.WOL.value)
),
): SelectSelector(_dict_to_select(POWER_ON_METHODS)),
vol.Required(
CONF_POWER_ON_SERVICE_DATA,
default=options.get(
CONF_POWER_ON_SERVICE_DATA, "{}"
),
): cv.string,
Comment on lines +489 to +500

@ollo69 ollo69 Dec 13, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this way we are going to provide a SmartThings option also if SmartThings is not enabled (not good) and we also add a text field that should be useless in some cases. This will probably confuse "normal" user and cause false issues, because I'm trying to simplify option flow (I will introduce some changes shortly), I do not like very match this approach.

I suggest to add only the text field in the advanced option:

  • if it contain some values, the power-on based on service will be used (may be you could also hide the power on method field in main form in this case)
  • if empty, everything will normally

vol.Required(CONF_SHOW_ADV_OPT, default=False): bool,
}

Expand All @@ -501,12 +515,6 @@ def _async_option_form(self):
CONF_SHOW_CHANNEL_NR,
default=options.get(CONF_SHOW_CHANNEL_NR, False),
): bool,
vol.Required(
CONF_POWER_ON_METHOD,
default=options.get(
CONF_POWER_ON_METHOD, str(PowerOnMethod.WOL.value)
),
): SelectSelector(_dict_to_select(POWER_ON_METHODS)),
}

data_schema.update(opt_schema)
Expand Down
2 changes: 2 additions & 0 deletions custom_components/samsungtv_smart/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppLaunchMethod(Enum):
class PowerOnMethod(Enum):
WOL = 1
SmartThings = 2
Service = 3


DOMAIN = "samsungtv_smart"
Expand Down Expand Up @@ -49,6 +50,7 @@ class PowerOnMethod(Enum):
CONF_PING_PORT = "ping_port"
CONF_POWER_ON_DELAY = "power_on_delay"
CONF_POWER_ON_METHOD = "power_on_method"
CONF_POWER_ON_SERVICE_DATA = "power_on_service_data"
CONF_SHOW_CHANNEL_NR = "show_channel_number"
CONF_SOURCE_LIST = "source_list"
CONF_SYNC_TURN_OFF = "sync_turn_off"
Expand Down
6 changes: 6 additions & 0 deletions custom_components/samsungtv_smart/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
CONF_PING_PORT,
CONF_POWER_ON_DELAY,
CONF_POWER_ON_METHOD,
CONF_POWER_ON_SERVICE_DATA,
CONF_SHOW_CHANNEL_NR,
CONF_SOURCE_LIST,
CONF_SYNC_TURN_OFF,
Expand Down Expand Up @@ -1177,6 +1178,11 @@ async def _async_power_on(self, set_art_mode=False):

if turn_on_method == PowerOnMethod.SmartThings and self._st:
await self._st.async_turn_on()
elif turn_on_method == PowerOnMethod.Service:
service_data = json.loads(self._get_option(CONF_POWER_ON_SERVICE_DATA, "{}"))
await async_call_from_config(
self.hass, service_data, blocking=False, validate_config=True,
)
else:
result = await self.hass.async_add_executor_job(self._send_wol_packet)

Expand Down