Make Philips TV notify service optional (#50691)

* Make event service optional

* Correct strings for rename

* Drop unload we are just testing config flow here
This commit is contained in:
Joakim Plate 2021-06-27 22:57:08 +02:00 committed by GitHub
parent c404a196c2
commit 0d689eefd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 11 deletions

View File

@ -20,7 +20,7 @@ from homeassistant.core import CALLBACK_TYPE, Context, HassJob, HomeAssistant, c
from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN from .const import CONF_ALLOW_NOTIFY, DOMAIN
PLATFORMS = ["media_player", "light", "remote"] PLATFORMS = ["media_player", "light", "remote"]
@ -36,8 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
username=entry.data.get(CONF_USERNAME), username=entry.data.get(CONF_USERNAME),
password=entry.data.get(CONF_PASSWORD), password=entry.data.get(CONF_PASSWORD),
) )
coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi, entry.options)
coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi)
await coordinator.async_refresh() await coordinator.async_refresh()
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
@ -45,9 +44,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.config_entries.async_setup_platforms(entry, PLATFORMS) hass.config_entries.async_setup_platforms(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_update_entry))
return True return True
async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update options."""
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
@ -94,9 +100,10 @@ class PluggableAction:
class PhilipsTVDataUpdateCoordinator(DataUpdateCoordinator[None]): class PhilipsTVDataUpdateCoordinator(DataUpdateCoordinator[None]):
"""Coordinator to update data.""" """Coordinator to update data."""
def __init__(self, hass, api: PhilipsTV) -> None: def __init__(self, hass, api: PhilipsTV, options: dict) -> None:
"""Set up the coordinator.""" """Set up the coordinator."""
self.api = api self.api = api
self.options = options
self._notify_future: asyncio.Task | None = None self._notify_future: asyncio.Task | None = None
@callback @callback
@ -127,6 +134,7 @@ class PhilipsTVDataUpdateCoordinator(DataUpdateCoordinator[None]):
self.api.on self.api.on
and self.api.powerstate == "On" and self.api.powerstate == "On"
and self.api.notify_change_supported and self.api.notify_change_supported
and self.options.get(CONF_ALLOW_NOTIFY, False)
) )
async def _notify_task(self): async def _notify_task(self):

View File

@ -17,7 +17,7 @@ from homeassistant.const import (
) )
from . import LOGGER from . import LOGGER
from .const import CONF_SYSTEM, CONST_APP_ID, CONST_APP_NAME, DOMAIN from .const import CONF_ALLOW_NOTIFY, CONF_SYSTEM, CONST_APP_ID, CONST_APP_NAME, DOMAIN
async def validate_input( async def validate_input(
@ -154,3 +154,32 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
} }
) )
return self.async_show_form(step_id="user", data_schema=schema, errors=errors) return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
@staticmethod
@core.callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a option flow for AEMET."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
"""Handle options flow."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
data_schema = vol.Schema(
{
vol.Required(
CONF_ALLOW_NOTIFY,
default=self.config_entry.options.get(CONF_ALLOW_NOTIFY),
): bool,
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)

View File

@ -2,6 +2,7 @@
DOMAIN = "philips_js" DOMAIN = "philips_js"
CONF_SYSTEM = "system" CONF_SYSTEM = "system"
CONF_ALLOW_NOTIFY = "allow_notify"
CONST_APP_ID = "homeassistant.io" CONST_APP_ID = "homeassistant.io"
CONST_APP_NAME = "Home Assistant" CONST_APP_NAME = "Home Assistant"

View File

@ -20,11 +20,20 @@
"unknown": "[%key:common::config_flow::error::unknown%]", "unknown": "[%key:common::config_flow::error::unknown%]",
"pairing_failure": "Unable to pair: {error_id}", "pairing_failure": "Unable to pair: {error_id}",
"invalid_pin": "Invalid PIN" "invalid_pin": "Invalid PIN"
}, },
"abort": { "abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]" "already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
} }
}, },
"options": {
"step": {
"init": {
"data": {
"allow_notify": "Allow usage of data notification service."
}
}
}
},
"device_automation": { "device_automation": {
"trigger_type": { "trigger_type": {
"turn_on": "Device is requested to turn on" "turn_on": "Device is requested to turn on"

View File

@ -25,6 +25,15 @@
} }
} }
}, },
"options": {
"step": {
"init": {
"data": {
"allow_notify": "Allow usage of data notification service."
}
}
}
},
"device_automation": { "device_automation": {
"trigger_type": { "trigger_type": {
"turn_on": "Device is requested to turn on" "turn_on": "Device is requested to turn on"

View File

@ -4,8 +4,8 @@ from unittest.mock import ANY, patch
from haphilipsjs import PairingFailure from haphilipsjs import PairingFailure
from pytest import fixture from pytest import fixture
from homeassistant import config_entries from homeassistant import config_entries, data_entry_flow
from homeassistant.components.philips_js.const import DOMAIN from homeassistant.components.philips_js.const import CONF_ALLOW_NOTIFY, DOMAIN
from . import ( from . import (
MOCK_CONFIG, MOCK_CONFIG,
@ -17,13 +17,17 @@ from . import (
MOCK_USERNAME, MOCK_USERNAME,
) )
from tests.common import MockConfigEntry
@fixture(autouse=True)
def mock_setup_entry(): @fixture(autouse=True, name="mock_setup_entry")
def mock_setup_entry_fixture():
"""Disable component setup.""" """Disable component setup."""
with patch( with patch(
"homeassistant.components.philips_js.async_setup_entry", return_value=True "homeassistant.components.philips_js.async_setup_entry", return_value=True
) as mock_setup_entry: ) as mock_setup_entry, patch(
"homeassistant.components.philips_js.async_unload_entry", return_value=True
):
yield mock_setup_entry yield mock_setup_entry
@ -226,3 +230,28 @@ async def test_pair_grant_failed(hass, mock_tv_pairable, mock_setup_entry):
"reason": "pairing_failure", "reason": "pairing_failure",
"type": "abort", "type": "abort",
} }
async def test_options_flow(hass):
"""Test config flow options."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="123456",
data=MOCK_CONFIG_PAIRED,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_ALLOW_NOTIFY: True}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_ALLOW_NOTIFY: True}