Use entity service for motion blinds (#44611)

* Simplify motion blinds service
* Switch to using entity service
pull/44619/head
starkillerOG 2020-12-29 16:21:51 +01:00 committed by GitHub
parent 4905be0c40
commit 24f6f59eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 59 deletions

View File

@ -5,65 +5,28 @@ import logging
from socket import timeout
from motionblinds import MotionMulticast
import voluptuous as vol
from homeassistant import config_entries, core
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_API_KEY,
CONF_HOST,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.const import CONF_API_KEY, CONF_HOST, EVENT_HOMEASSISTANT_STOP
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
ATTR_ABSOLUTE_POSITION,
ATTR_WIDTH,
DOMAIN,
KEY_COORDINATOR,
KEY_GATEWAY,
KEY_MULTICAST_LISTENER,
MANUFACTURER,
MOTION_PLATFORMS,
SERVICE_SET_ABSOLUTE_POSITION,
)
from .gateway import ConnectMotionGateway
_LOGGER = logging.getLogger(__name__)
CALL_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids})
SET_ABSOLUTE_POSITION_SCHEMA = CALL_SCHEMA.extend(
{
vol.Required(ATTR_ABSOLUTE_POSITION): vol.All(
cv.positive_int, vol.Range(max=100)
),
vol.Optional(ATTR_WIDTH): vol.All(cv.positive_int, vol.Range(max=100)),
}
)
SERVICE_TO_METHOD = {
SERVICE_SET_ABSOLUTE_POSITION: {
"schema": SET_ABSOLUTE_POSITION_SCHEMA,
}
}
def setup(hass: core.HomeAssistant, config: dict):
"""Set up the Motion Blinds component."""
def service_handler(service):
data = service.data.copy()
data["method"] = service.service
dispatcher_send(hass, DOMAIN, data)
for service in SERVICE_TO_METHOD:
schema = SERVICE_TO_METHOD[service]["schema"]
hass.services.register(DOMAIN, service, service_handler, schema=schema)
return True

View File

@ -3,6 +3,7 @@
import logging
from motionblinds import BlindType
import voluptuous as vol
from homeassistant.components.cover import (
ATTR_POSITION,
@ -15,8 +16,7 @@ from homeassistant.components.cover import (
DEVICE_CLASS_SHUTTER,
CoverEntity,
)
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, ENTITY_MATCH_NONE
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
@ -26,6 +26,7 @@ from .const import (
KEY_COORDINATOR,
KEY_GATEWAY,
MANUFACTURER,
SERVICE_SET_ABSOLUTE_POSITION,
)
_LOGGER = logging.getLogger(__name__)
@ -57,6 +58,12 @@ TDBU_DEVICE_MAP = {
}
SET_ABSOLUTE_POSITION_SCHEMA = {
vol.Required(ATTR_ABSOLUTE_POSITION): vol.All(cv.positive_int, vol.Range(max=100)),
vol.Optional(ATTR_WIDTH): vol.All(cv.positive_int, vol.Range(max=100)),
}
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Motion Blind from a config entry."""
entities = []
@ -108,6 +115,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(entities)
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SERVICE_SET_ABSOLUTE_POSITION,
SET_ABSOLUTE_POSITION_SCHEMA,
SERVICE_SET_ABSOLUTE_POSITION,
)
class MotionPositionDevice(CoordinatorEntity, CoverEntity):
"""Representation of a Motion Blind Device."""
@ -172,26 +186,8 @@ class MotionPositionDevice(CoordinatorEntity, CoverEntity):
async def async_added_to_hass(self):
"""Subscribe to multicast pushes and register signal handler."""
self._blind.Register_callback(self.unique_id, self.schedule_update_ha_state)
self.async_on_remove(
async_dispatcher_connect(self.hass, DOMAIN, self.signal_handler)
)
await super().async_added_to_hass()
def signal_handler(self, data):
"""Handle domain-specific signal by calling appropriate method."""
entity_ids = data[ATTR_ENTITY_ID]
if entity_ids == ENTITY_MATCH_NONE:
return
if entity_ids == ENTITY_MATCH_ALL or self.entity_id in entity_ids:
params = {
key: value
for key, value in data.items()
if key not in ["entity_id", "method"]
}
getattr(self, data["method"])(**params)
async def async_will_remove_from_hass(self):
"""Unsubscribe when removed."""
self._blind.Remove_callback(self.unique_id)