Skip to content

Commit

Permalink
🎄 fix services
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Dec 25, 2020
1 parent cfb340c commit b5c40e3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
72 changes: 56 additions & 16 deletions custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Xiaomi Miot."""
import logging
import asyncio
from math import ceil
from datetime import timedelta
from functools import partial
Expand Down Expand Up @@ -47,32 +48,35 @@
},
)


async def async_setup(hass, config: dict):
hass.data.setdefault(DOMAIN, {})
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
hass.data[DOMAIN]['component'] = component
await component.async_setup(config)
component.async_register_entity_service(
'send_command',
XIAOMI_MIIO_SERVICE_SCHEMA.extend(
SERVICE_TO_METHOD_BASE = {
'send_command': {
'method': 'async_command',
'schema': XIAOMI_MIIO_SERVICE_SCHEMA.extend(
{
vol.Required('method'): cv.string,
vol.Optional('params', default = []): cv.ensure_list,
},
),
'async_command'
)
component.async_register_entity_service(
'set_property',
XIAOMI_MIIO_SERVICE_SCHEMA.extend(
},
'set_property': {
'method': 'async_set_property',
'schema': XIAOMI_MIIO_SERVICE_SCHEMA.extend(
{
vol.Required('field'): cv.string,
vol.Required('value'): cv.match_all,
},
),
'async_set_property'
)
},
}


async def async_setup(hass, config: dict):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN].setdefault('entities', {})
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
hass.data[DOMAIN]['component'] = component
await component.async_setup(config)
bind_services_to_entries(hass, SERVICE_TO_METHOD_BASE)
return True

async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry):
Expand Down Expand Up @@ -110,6 +114,42 @@ async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entri
return True


def bind_services_to_entries(hass, services):
async def async_service_handler(service):
method = services.get(service.service)
fun = method['method']
params = {
key: value
for key, value in service.data.items()
if key != ATTR_ENTITY_ID
}
target_devices = []
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
target_devices = [
dvc
for dvc in hass.data[DOMAIN]['entities'].values()
if dvc.entity_id in entity_ids
]
_LOGGER.debug('Xiaomi Miot async_service_handler %s', {
'targets' : [ dvc.entity_id for dvc in target_devices ],
'method' : fun,
'params' : params,
})
update_tasks = []
for dvc in target_devices:
if not hasattr(dvc, fun):
_LOGGER.info('%s have no method: %s', dvc.entity_id, fun)
continue
await getattr(dvc, fun)(**params)
update_tasks.append(dvc.async_update_ha_state(True))
if update_tasks:
await asyncio.wait(update_tasks)
for srv, obj in services.items():
schema = obj.get('schema', XIAOMI_MIIO_SERVICE_SCHEMA)
hass.services.async_register(DOMAIN, srv, async_service_handler, schema = schema)


class MiioEntity(ToggleEntity):
def __init__(self, name, device):
self._device = device
Expand Down
21 changes: 11 additions & 10 deletions custom_components/xiaomi_miot/climate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Support for Xiaomi Aircondition."""
import logging
import asyncio
import voluptuous as vol
from enum import Enum
from functools import partial
Expand All @@ -21,6 +20,7 @@
PLATFORM_SCHEMA,
XIAOMI_MIIO_SERVICE_SCHEMA,
MiotEntity,
bind_services_to_entries,
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -29,7 +29,13 @@
DEFAULT_MIN_TEMP = 16.0
DEFAULT_MAX_TEMP = 31.0

async def async_add_entities_from_config(hass, config, async_add_entities):
SERVICE_TO_METHOD = {}

async def async_setup_entry(hass, config_entry, async_add_entities):
config = hass.data[DOMAIN]['configs'].get(config_entry.entry_id,config_entry.data)
await async_setup_platform(hass, config, async_add_entities)

async def async_setup_platform(hass, config, async_add_entities, discovery_info = None):
if DATA_KEY not in hass.data:
hass.data[DATA_KEY] = {}
host = config[CONF_HOST]
Expand All @@ -38,15 +44,10 @@ async def async_add_entities_from_config(hass, config, async_add_entities):
if 1:
entity = MiotClimateEntity(config)
entities.append(entity)
hass.data[DATA_KEY][host] = entity
for entity in entities:
hass.data[DOMAIN]['entities'][entity.unique_id] = entity
async_add_entities(entities, update_before_add = True)

async def async_setup_entry(hass, config_entry, async_add_entities):
config = hass.data[DOMAIN]['configs'].get(config_entry.entry_id,config_entry.data)
await async_add_entities_from_config(hass, config, async_add_entities)

async def async_setup_platform(hass, config, async_add_entities, discovery_info = None):
await async_add_entities_from_config(hass, config, async_add_entities)
bind_services_to_entries(hass, SERVICE_TO_METHOD)


HvacModes = Enum('HvacModes',{
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set_property:
fields:
entity_id:
description: Name of the entity.
example: "xiaomi.aircondition.mt5"
example: "climate.xiaomi_mc5_374e"
field:
description: Field of property.
example: power
Expand Down

0 comments on commit b5c40e3

Please sign in to comment.