mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add service to turn on the switcher device with timer (#33235)
* Update services.yaml * Update __init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/switcher_kis/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update __init__.py * Update services.yaml Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
e78ceab560
commit
c37100299a
@ -7,11 +7,12 @@ from typing import Dict, Optional
|
||||
|
||||
from aioswitcher.api import SwitcherV2Api
|
||||
from aioswitcher.bridge import SwitcherV2Bridge
|
||||
from aioswitcher.consts import COMMAND_ON
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.auth.permissions.const import POLICY_EDIT
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import CONF_ENTITY_ID, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import callback, split_entity_id
|
||||
from homeassistant.exceptions import Unauthorized, UnknownUser
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
@ -32,6 +33,7 @@ _LOGGER = getLogger(__name__)
|
||||
DOMAIN = "switcher_kis"
|
||||
|
||||
CONF_AUTO_OFF = "auto_off"
|
||||
CONF_TIMER_MINUTES = "timer_minutes"
|
||||
CONF_DEVICE_ID = "device_id"
|
||||
CONF_DEVICE_PASSWORD = "device_password"
|
||||
CONF_PHONE_ID = "phone_id"
|
||||
@ -60,11 +62,21 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
SERVICE_SET_AUTO_OFF_NAME = "set_auto_off"
|
||||
SERVICE_SET_AUTO_OFF_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_AUTO_OFF): cv.time_period_str,
|
||||
}
|
||||
)
|
||||
|
||||
SERVICE_TURN_ON_WITH_TIMER_NAME = "turn_on_with_timer"
|
||||
SERVICE_TURN_ON_WITH_TIMER_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TIMER_MINUTES): vol.All(
|
||||
cv.positive_int, vol.Range(min=1, max=90)
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def _validate_edit_permission(
|
||||
@ -76,13 +88,11 @@ async def _validate_edit_permission(
|
||||
raise Unauthorized(
|
||||
context=context, entity_id=entity_id, permission=(POLICY_EDIT,)
|
||||
)
|
||||
|
||||
user = await hass.auth.async_get_user(context.user_id)
|
||||
if user is None:
|
||||
raise UnknownUser(
|
||||
context=context, entity_id=entity_id, permission=(POLICY_EDIT,)
|
||||
)
|
||||
|
||||
if not user.permissions.check_entity(entity_id, POLICY_EDIT):
|
||||
raise Unauthorized(
|
||||
context=context, entity_id=entity_id, permission=(POLICY_EDIT,)
|
||||
@ -112,7 +122,6 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
|
||||
_LOGGER.exception("Failed to get response from device")
|
||||
await v2bridge.stop()
|
||||
return False
|
||||
|
||||
hass.data[DOMAIN] = {DATA_DEVICE: device_data}
|
||||
|
||||
async def async_switch_platform_discovered(
|
||||
@ -126,7 +135,7 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
|
||||
"""Use for handling setting device auto-off service calls."""
|
||||
|
||||
await _validate_edit_permission(
|
||||
hass, service.context, service.data[CONF_ENTITY_ID]
|
||||
hass, service.context, service.data[ATTR_ENTITY_ID]
|
||||
)
|
||||
|
||||
async with SwitcherV2Api(
|
||||
@ -134,6 +143,18 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
|
||||
) as swapi:
|
||||
await swapi.set_auto_shutdown(service.data[CONF_AUTO_OFF])
|
||||
|
||||
async def async_turn_on_with_timer_service(service: ServiceCallType) -> None:
|
||||
"""Use for handling turning device on with a timer service calls."""
|
||||
|
||||
await _validate_edit_permission(
|
||||
hass, service.context, service.data[ATTR_ENTITY_ID]
|
||||
)
|
||||
|
||||
async with SwitcherV2Api(
|
||||
hass.loop, device_data.ip_addr, phone_id, device_id, device_password
|
||||
) as swapi:
|
||||
await swapi.control_device(COMMAND_ON, service.data[CONF_TIMER_MINUTES])
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_SET_AUTO_OFF_NAME,
|
||||
@ -141,6 +162,13 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
|
||||
schema=SERVICE_SET_AUTO_OFF_SCHEMA,
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_TURN_ON_WITH_TIMER_NAME,
|
||||
async_turn_on_with_timer_service,
|
||||
schema=SERVICE_TURN_ON_WITH_TIMER_SCHEMA,
|
||||
)
|
||||
|
||||
async_listen_platform(hass, SWITCH_DOMAIN, async_switch_platform_discovered)
|
||||
|
||||
hass.async_create_task(async_load_platform(hass, SWITCH_DOMAIN, DOMAIN, {}, config))
|
||||
|
@ -7,3 +7,13 @@ set_auto_off:
|
||||
auto_off:
|
||||
description: "Time period string containing hours and minutes."
|
||||
example: '"02:30"'
|
||||
|
||||
turn_on_with_timer:
|
||||
description: 'Turn on the Switcher device with timer.'
|
||||
fields:
|
||||
entity_id:
|
||||
description: "Name of the entity id associated with the integration, used for permission validation."
|
||||
example: "switch.switcher_kis_boiler"
|
||||
timer_minutes:
|
||||
description: 'Minutes to turn on (valid range from 1 to 90)'
|
||||
example: '30'
|
||||
|
Loading…
x
Reference in New Issue
Block a user