Add timer support to mobile app (#121469)
* Add timer support to mobile app * Fix tests * Make it time-sensitive
This commit is contained in:
parent
4ae6e38800
commit
454ca0ce95
4 changed files with 139 additions and 3 deletions
|
@ -1,9 +1,10 @@
|
|||
"""Integrates Native Apps to Home Assistant."""
|
||||
|
||||
from contextlib import suppress
|
||||
from functools import partial
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components import cloud, notify as hass_notify
|
||||
from homeassistant.components import cloud, intent, notify as hass_notify
|
||||
from homeassistant.components.webhook import (
|
||||
async_register as webhook_register,
|
||||
async_unregister as webhook_unregister,
|
||||
|
@ -46,7 +47,8 @@ from .const import (
|
|||
)
|
||||
from .helpers import savable_state
|
||||
from .http_api import RegistrationsView
|
||||
from .util import async_create_cloud_hook
|
||||
from .timers import async_handle_timer_event
|
||||
from .util import async_create_cloud_hook, supports_push
|
||||
from .webhook import handle_webhook
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR]
|
||||
|
@ -133,6 +135,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
if supports_push(hass, webhook_id):
|
||||
entry.async_on_unload(
|
||||
intent.async_register_timer_handler(
|
||||
hass, device.id, partial(async_handle_timer_event, hass, entry)
|
||||
)
|
||||
)
|
||||
|
||||
await hass_notify.async_reload(hass, DOMAIN)
|
||||
|
||||
return True
|
||||
|
|
|
@ -4,7 +4,14 @@
|
|||
"after_dependencies": ["cloud", "camera", "conversation", "notify"],
|
||||
"codeowners": ["@home-assistant/core"],
|
||||
"config_flow": true,
|
||||
"dependencies": ["http", "webhook", "person", "tag", "websocket_api"],
|
||||
"dependencies": [
|
||||
"http",
|
||||
"intent",
|
||||
"person",
|
||||
"tag",
|
||||
"webhook",
|
||||
"websocket_api"
|
||||
],
|
||||
"documentation": "https://www.home-assistant.io/integrations/mobile_app",
|
||||
"iot_class": "local_push",
|
||||
"loggers": ["nacl"],
|
||||
|
|
52
homeassistant/components/mobile_app/timers.py
Normal file
52
homeassistant/components/mobile_app/timers.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
"""Timers for the mobile app."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components import notify
|
||||
from homeassistant.components.intent.timers import TimerEventType, TimerInfo
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_DEVICE_ID
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from . import device_action
|
||||
|
||||
|
||||
@callback
|
||||
def async_handle_timer_event(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
event_type: TimerEventType,
|
||||
timer_info: TimerInfo,
|
||||
) -> None:
|
||||
"""Handle timer events."""
|
||||
if event_type != TimerEventType.FINISHED:
|
||||
return
|
||||
|
||||
if timer_info.name:
|
||||
message = f"{timer_info.name} finished"
|
||||
else:
|
||||
message = f"{timedelta(seconds=timer_info.created_seconds)} timer finished"
|
||||
|
||||
entry.async_create_task(
|
||||
hass,
|
||||
device_action.async_call_action_from_config(
|
||||
hass,
|
||||
{
|
||||
CONF_DEVICE_ID: timer_info.device_id,
|
||||
notify.ATTR_MESSAGE: message,
|
||||
notify.ATTR_DATA: {
|
||||
"group": "timers",
|
||||
# Android
|
||||
"channel": "Timers",
|
||||
"importance": "high",
|
||||
# iOS
|
||||
"push": {
|
||||
"interruption-level": "time-sensitive",
|
||||
},
|
||||
},
|
||||
},
|
||||
{},
|
||||
None,
|
||||
),
|
||||
"mobile_app_timer_notification",
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue