mirror of
https://github.com/andvikt/mega_hacs.git
synced 2025-12-11 17:14:28 +05:00
fix services
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
"""The mega integration."""
|
"""The mega integration."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_ID
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_ID
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, ServiceCall
|
||||||
from homeassistant.helpers.service import bind_hass
|
from homeassistant.helpers.service import bind_hass
|
||||||
from homeassistant.components import mqtt
|
from homeassistant.components import mqtt
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@@ -49,18 +50,18 @@ async def async_setup(hass: HomeAssistant, config: dict):
|
|||||||
conf = config.get(DOMAIN)
|
conf = config.get(DOMAIN)
|
||||||
hass.data[DOMAIN] = {}
|
hass.data[DOMAIN] = {}
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, 'save', _save_service, schema=vol.Schema({
|
DOMAIN, 'save', partial(_save_service, hass), schema=vol.Schema({
|
||||||
vol.Optional('mega_id'): str
|
vol.Optional('mega_id'): str
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, 'get_port', _get_port, schema=vol.Schema({
|
DOMAIN, 'get_port', partial(_get_port, hass), schema=vol.Schema({
|
||||||
vol.Optional('mega_id'): str,
|
vol.Optional('mega_id'): str,
|
||||||
vol.Optional('port'): int,
|
vol.Optional('port'): int,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, 'run_cmd', _run_cmd, schema=vol.Schema({
|
DOMAIN, 'run_cmd', partial(_run_cmd, hass), schema=vol.Schema({
|
||||||
vol.Required('port'): int,
|
vol.Required('port'): int,
|
||||||
vol.Required('cmd'): str,
|
vol.Required('cmd'): str,
|
||||||
vol.Optional('mega_id'): str,
|
vol.Optional('mega_id'): str,
|
||||||
@@ -131,8 +132,8 @@ async def async_remove_entry(hass, entry) -> None:
|
|||||||
unsub()
|
unsub()
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
async def _save_service(hass: HomeAssistant, call: ServiceCall):
|
||||||
async def _save_service(hass: HomeAssistant, mega_id=None):
|
mega_id = call.data['mega_id']
|
||||||
if mega_id:
|
if mega_id:
|
||||||
hub: MegaD = hass.data[DOMAIN][mega_id]
|
hub: MegaD = hass.data[DOMAIN][mega_id]
|
||||||
await hub.save()
|
await hub.save()
|
||||||
@@ -142,7 +143,9 @@ async def _save_service(hass: HomeAssistant, mega_id=None):
|
|||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
async def _get_port(hass: HomeAssistant, port=None, mega_id=None):
|
async def _get_port(hass: HomeAssistant, call: ServiceCall):
|
||||||
|
port = call.data['port']
|
||||||
|
mega_id = call.data['mega_id']
|
||||||
if mega_id:
|
if mega_id:
|
||||||
hub: MegaD = hass.data[DOMAIN][mega_id]
|
hub: MegaD = hass.data[DOMAIN][mega_id]
|
||||||
if port is None:
|
if port is None:
|
||||||
@@ -156,8 +159,12 @@ async def _get_port(hass: HomeAssistant, port=None, mega_id=None):
|
|||||||
else:
|
else:
|
||||||
await hub.get_port(port)
|
await hub.get_port(port)
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
async def _run_cmd(hass: HomeAssistant, port: int, cmd: str, mega_id=None):
|
async def _run_cmd(hass: HomeAssistant, call: ServiceCall):
|
||||||
|
port = call.data['port']
|
||||||
|
mega_id = call.data['mega_id']
|
||||||
|
cmd = call.data['cmd']
|
||||||
if mega_id:
|
if mega_id:
|
||||||
hub: MegaD = hass.data[DOMAIN][mega_id]
|
hub: MegaD = hass.data[DOMAIN][mega_id]
|
||||||
await hub.send_command(port=port, cmd=cmd)
|
await hub.send_command(port=port, cmd=cmd)
|
||||||
|
|||||||
Reference in New Issue
Block a user