mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Move abode services to separate module (#146142)
* Move abode services to separate module * Rename * Adjust test imports
This commit is contained in:
parent
fd93cf375d
commit
2bd3196183
@ -14,30 +14,24 @@ from jaraco.abode.exceptions import (
|
|||||||
)
|
)
|
||||||
from jaraco.abode.helpers.timeline import Groups as GROUPS
|
from jaraco.abode.helpers.timeline import Groups as GROUPS
|
||||||
from requests.exceptions import ConnectTimeout, HTTPError
|
from requests.exceptions import ConnectTimeout, HTTPError
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_DATE,
|
ATTR_DATE,
|
||||||
ATTR_DEVICE_ID,
|
ATTR_DEVICE_ID,
|
||||||
ATTR_ENTITY_ID,
|
|
||||||
ATTR_TIME,
|
ATTR_TIME,
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, ServiceCall
|
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import dispatcher_send
|
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from .const import CONF_POLLING, DOMAIN, LOGGER
|
from .const import CONF_POLLING, DOMAIN, LOGGER
|
||||||
|
from .services import async_setup_services
|
||||||
SERVICE_SETTINGS = "change_setting"
|
|
||||||
SERVICE_CAPTURE_IMAGE = "capture_image"
|
|
||||||
SERVICE_TRIGGER_AUTOMATION = "trigger_automation"
|
|
||||||
|
|
||||||
ATTR_DEVICE_NAME = "device_name"
|
ATTR_DEVICE_NAME = "device_name"
|
||||||
ATTR_DEVICE_TYPE = "device_type"
|
ATTR_DEVICE_TYPE = "device_type"
|
||||||
@ -45,22 +39,12 @@ ATTR_EVENT_CODE = "event_code"
|
|||||||
ATTR_EVENT_NAME = "event_name"
|
ATTR_EVENT_NAME = "event_name"
|
||||||
ATTR_EVENT_TYPE = "event_type"
|
ATTR_EVENT_TYPE = "event_type"
|
||||||
ATTR_EVENT_UTC = "event_utc"
|
ATTR_EVENT_UTC = "event_utc"
|
||||||
ATTR_SETTING = "setting"
|
|
||||||
ATTR_USER_NAME = "user_name"
|
ATTR_USER_NAME = "user_name"
|
||||||
ATTR_APP_TYPE = "app_type"
|
ATTR_APP_TYPE = "app_type"
|
||||||
ATTR_EVENT_BY = "event_by"
|
ATTR_EVENT_BY = "event_by"
|
||||||
ATTR_VALUE = "value"
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||||
|
|
||||||
CHANGE_SETTING_SCHEMA = vol.Schema(
|
|
||||||
{vol.Required(ATTR_SETTING): cv.string, vol.Required(ATTR_VALUE): cv.string}
|
|
||||||
)
|
|
||||||
|
|
||||||
CAPTURE_IMAGE_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
|
|
||||||
|
|
||||||
AUTOMATION_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
|
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
Platform.ALARM_CONTROL_PANEL,
|
Platform.ALARM_CONTROL_PANEL,
|
||||||
Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
@ -85,7 +69,7 @@ class AbodeSystem:
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the Abode component."""
|
"""Set up the Abode component."""
|
||||||
setup_hass_services(hass)
|
async_setup_services(hass)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -138,60 +122,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
def setup_hass_services(hass: HomeAssistant) -> None:
|
|
||||||
"""Home Assistant services."""
|
|
||||||
|
|
||||||
def change_setting(call: ServiceCall) -> None:
|
|
||||||
"""Change an Abode system setting."""
|
|
||||||
setting = call.data[ATTR_SETTING]
|
|
||||||
value = call.data[ATTR_VALUE]
|
|
||||||
|
|
||||||
try:
|
|
||||||
hass.data[DOMAIN].abode.set_setting(setting, value)
|
|
||||||
except AbodeException as ex:
|
|
||||||
LOGGER.warning(ex)
|
|
||||||
|
|
||||||
def capture_image(call: ServiceCall) -> None:
|
|
||||||
"""Capture a new image."""
|
|
||||||
entity_ids = call.data[ATTR_ENTITY_ID]
|
|
||||||
|
|
||||||
target_entities = [
|
|
||||||
entity_id
|
|
||||||
for entity_id in hass.data[DOMAIN].entity_ids
|
|
||||||
if entity_id in entity_ids
|
|
||||||
]
|
|
||||||
|
|
||||||
for entity_id in target_entities:
|
|
||||||
signal = f"abode_camera_capture_{entity_id}"
|
|
||||||
dispatcher_send(hass, signal)
|
|
||||||
|
|
||||||
def trigger_automation(call: ServiceCall) -> None:
|
|
||||||
"""Trigger an Abode automation."""
|
|
||||||
entity_ids = call.data[ATTR_ENTITY_ID]
|
|
||||||
|
|
||||||
target_entities = [
|
|
||||||
entity_id
|
|
||||||
for entity_id in hass.data[DOMAIN].entity_ids
|
|
||||||
if entity_id in entity_ids
|
|
||||||
]
|
|
||||||
|
|
||||||
for entity_id in target_entities:
|
|
||||||
signal = f"abode_trigger_automation_{entity_id}"
|
|
||||||
dispatcher_send(hass, signal)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_SETTINGS, change_setting, schema=CHANGE_SETTING_SCHEMA
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_CAPTURE_IMAGE, capture_image, schema=CAPTURE_IMAGE_SCHEMA
|
|
||||||
)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_TRIGGER_AUTOMATION, trigger_automation, schema=AUTOMATION_SCHEMA
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def setup_hass_events(hass: HomeAssistant) -> None:
|
async def setup_hass_events(hass: HomeAssistant) -> None:
|
||||||
"""Home Assistant start and stop callbacks."""
|
"""Home Assistant start and stop callbacks."""
|
||||||
|
|
||||||
|
89
homeassistant/components/abode/services.py
Normal file
89
homeassistant/components/abode/services.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
"""Support for the Abode Security System."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from jaraco.abode.exceptions import Exception as AbodeException
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
||||||
|
|
||||||
|
from .const import DOMAIN, LOGGER
|
||||||
|
|
||||||
|
SERVICE_SETTINGS = "change_setting"
|
||||||
|
SERVICE_CAPTURE_IMAGE = "capture_image"
|
||||||
|
SERVICE_TRIGGER_AUTOMATION = "trigger_automation"
|
||||||
|
|
||||||
|
ATTR_SETTING = "setting"
|
||||||
|
ATTR_VALUE = "value"
|
||||||
|
|
||||||
|
|
||||||
|
CHANGE_SETTING_SCHEMA = vol.Schema(
|
||||||
|
{vol.Required(ATTR_SETTING): cv.string, vol.Required(ATTR_VALUE): cv.string}
|
||||||
|
)
|
||||||
|
|
||||||
|
CAPTURE_IMAGE_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
|
||||||
|
|
||||||
|
AUTOMATION_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
|
||||||
|
|
||||||
|
|
||||||
|
def _change_setting(call: ServiceCall) -> None:
|
||||||
|
"""Change an Abode system setting."""
|
||||||
|
setting = call.data[ATTR_SETTING]
|
||||||
|
value = call.data[ATTR_VALUE]
|
||||||
|
|
||||||
|
try:
|
||||||
|
call.hass.data[DOMAIN].abode.set_setting(setting, value)
|
||||||
|
except AbodeException as ex:
|
||||||
|
LOGGER.warning(ex)
|
||||||
|
|
||||||
|
|
||||||
|
def _capture_image(call: ServiceCall) -> None:
|
||||||
|
"""Capture a new image."""
|
||||||
|
entity_ids = call.data[ATTR_ENTITY_ID]
|
||||||
|
|
||||||
|
target_entities = [
|
||||||
|
entity_id
|
||||||
|
for entity_id in call.hass.data[DOMAIN].entity_ids
|
||||||
|
if entity_id in entity_ids
|
||||||
|
]
|
||||||
|
|
||||||
|
for entity_id in target_entities:
|
||||||
|
signal = f"abode_camera_capture_{entity_id}"
|
||||||
|
dispatcher_send(call.hass, signal)
|
||||||
|
|
||||||
|
|
||||||
|
def _trigger_automation(call: ServiceCall) -> None:
|
||||||
|
"""Trigger an Abode automation."""
|
||||||
|
entity_ids = call.data[ATTR_ENTITY_ID]
|
||||||
|
|
||||||
|
target_entities = [
|
||||||
|
entity_id
|
||||||
|
for entity_id in call.hass.data[DOMAIN].entity_ids
|
||||||
|
if entity_id in entity_ids
|
||||||
|
]
|
||||||
|
|
||||||
|
for entity_id in target_entities:
|
||||||
|
signal = f"abode_trigger_automation_{entity_id}"
|
||||||
|
dispatcher_send(call.hass, signal)
|
||||||
|
|
||||||
|
|
||||||
|
def async_setup_services(hass: HomeAssistant) -> None:
|
||||||
|
"""Home Assistant services."""
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_SETTINGS, _change_setting, schema=CHANGE_SETTING_SCHEMA
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN, SERVICE_CAPTURE_IMAGE, _capture_image, schema=CAPTURE_IMAGE_SCHEMA
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
SERVICE_TRIGGER_AUTOMATION,
|
||||||
|
_trigger_automation,
|
||||||
|
schema=AUTOMATION_SCHEMA,
|
||||||
|
)
|
@ -8,7 +8,8 @@ from jaraco.abode.exceptions import (
|
|||||||
Exception as AbodeException,
|
Exception as AbodeException,
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.components.abode import DOMAIN, SERVICE_SETTINGS
|
from homeassistant.components.abode.const import DOMAIN
|
||||||
|
from homeassistant.components.abode.services import SERVICE_SETTINGS
|
||||||
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
|
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import CONF_USERNAME
|
from homeassistant.const import CONF_USERNAME
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.abode import DOMAIN, SERVICE_TRIGGER_AUTOMATION
|
from homeassistant.components.abode.const import DOMAIN
|
||||||
|
from homeassistant.components.abode.services import SERVICE_TRIGGER_AUTOMATION
|
||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user