Clean startup of modbus by moving service schemas (#57763)
This commit is contained in:
parent
fb5d117df4
commit
42803e6ac0
3 changed files with 31 additions and 56 deletions
|
@ -49,11 +49,6 @@ import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_ADDRESS,
|
|
||||||
ATTR_HUB,
|
|
||||||
ATTR_STATE,
|
|
||||||
ATTR_UNIT,
|
|
||||||
ATTR_VALUE,
|
|
||||||
CALL_TYPE_COIL,
|
CALL_TYPE_COIL,
|
||||||
CALL_TYPE_DISCRETE,
|
CALL_TYPE_DISCRETE,
|
||||||
CALL_TYPE_REGISTER_HOLDING,
|
CALL_TYPE_REGISTER_HOLDING,
|
||||||
|
@ -334,33 +329,6 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
extra=vol.ALLOW_EXTRA,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
|
|
||||||
SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Optional(ATTR_HUB, default=DEFAULT_HUB): cv.string,
|
|
||||||
vol.Required(ATTR_UNIT): cv.positive_int,
|
|
||||||
vol.Required(ATTR_ADDRESS): cv.positive_int,
|
|
||||||
vol.Required(ATTR_VALUE): vol.Any(
|
|
||||||
cv.positive_int, vol.All(cv.ensure_list, [cv.positive_int])
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
SERVICE_WRITE_COIL_SCHEMA = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Optional(ATTR_HUB, default=DEFAULT_HUB): cv.string,
|
|
||||||
vol.Required(ATTR_UNIT): cv.positive_int,
|
|
||||||
vol.Required(ATTR_ADDRESS): cv.positive_int,
|
|
||||||
vol.Required(ATTR_STATE): vol.Any(
|
|
||||||
cv.boolean, vol.All(cv.ensure_list, [cv.boolean])
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
SERVICE_STOP_START_SCHEMA = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(ATTR_HUB): cv.string,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
||||||
"""Return modbus hub with name."""
|
"""Return modbus hub with name."""
|
||||||
|
@ -372,7 +340,4 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
return await async_modbus_setup(
|
return await async_modbus_setup(
|
||||||
hass,
|
hass,
|
||||||
config,
|
config,
|
||||||
SERVICE_WRITE_REGISTER_SCHEMA,
|
|
||||||
SERVICE_WRITE_COIL_SCHEMA,
|
|
||||||
SERVICE_STOP_START_SCHEMA,
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -69,6 +69,7 @@ SERIAL = "serial"
|
||||||
TCP = "tcp"
|
TCP = "tcp"
|
||||||
UDP = "udp"
|
UDP = "udp"
|
||||||
|
|
||||||
|
|
||||||
# service call attributes
|
# service call attributes
|
||||||
ATTR_ADDRESS = "address"
|
ATTR_ADDRESS = "address"
|
||||||
ATTR_HUB = "hub"
|
ATTR_HUB = "hub"
|
||||||
|
@ -78,7 +79,6 @@ ATTR_STATE = "state"
|
||||||
ATTR_TEMPERATURE = "temperature"
|
ATTR_TEMPERATURE = "temperature"
|
||||||
|
|
||||||
|
|
||||||
# data types
|
|
||||||
class DataType(str, Enum):
|
class DataType(str, Enum):
|
||||||
"""Data types used by sensor etc."""
|
"""Data types used by sensor etc."""
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.discovery import async_load_platform
|
from homeassistant.helpers.discovery import async_load_platform
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.event import Event, async_call_later
|
from homeassistant.helpers.event import Event, async_call_later
|
||||||
|
@ -124,9 +125,6 @@ PYMODBUS_CALL = [
|
||||||
async def async_modbus_setup(
|
async def async_modbus_setup(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
service_write_register_schema: vol.Schema,
|
|
||||||
service_write_coil_schema: vol.Schema,
|
|
||||||
service_stop_start_schema: vol.Schema,
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Set up Modbus component."""
|
"""Set up Modbus component."""
|
||||||
|
|
||||||
|
@ -173,13 +171,6 @@ async def async_modbus_setup(
|
||||||
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
|
unit, address, int(float(value)), CALL_TYPE_WRITE_REGISTER
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN,
|
|
||||||
SERVICE_WRITE_REGISTER,
|
|
||||||
async_write_register,
|
|
||||||
schema=service_write_register_schema,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_write_coil(service: ServiceCall) -> None:
|
async def async_write_coil(service: ServiceCall) -> None:
|
||||||
"""Write Modbus coil."""
|
"""Write Modbus coil."""
|
||||||
unit = service.data[ATTR_UNIT]
|
unit = service.data[ATTR_UNIT]
|
||||||
|
@ -193,9 +184,25 @@ async def async_modbus_setup(
|
||||||
else:
|
else:
|
||||||
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COIL)
|
await hub.async_pymodbus_call(unit, address, state, CALL_TYPE_WRITE_COIL)
|
||||||
|
|
||||||
hass.services.async_register(
|
for x_write in (
|
||||||
DOMAIN, SERVICE_WRITE_COIL, async_write_coil, schema=service_write_coil_schema
|
(SERVICE_WRITE_REGISTER, async_write_register, ATTR_VALUE, cv.positive_int),
|
||||||
)
|
(SERVICE_WRITE_COIL, async_write_coil, ATTR_STATE, cv.boolean),
|
||||||
|
):
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
x_write[0],
|
||||||
|
x_write[1],
|
||||||
|
schema=vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(ATTR_HUB, default=DEFAULT_HUB): cv.string,
|
||||||
|
vol.Required(ATTR_UNIT): cv.positive_int,
|
||||||
|
vol.Required(ATTR_ADDRESS): cv.positive_int,
|
||||||
|
vol.Required(x_write[2]): vol.Any(
|
||||||
|
cv.positive_int, vol.All(cv.ensure_list, [x_write[3]])
|
||||||
|
),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
async def async_stop_hub(service: ServiceCall) -> None:
|
async def async_stop_hub(service: ServiceCall) -> None:
|
||||||
"""Stop Modbus hub."""
|
"""Stop Modbus hub."""
|
||||||
|
@ -203,19 +210,22 @@ async def async_modbus_setup(
|
||||||
hub = hub_collect[service.data[ATTR_HUB]]
|
hub = hub_collect[service.data[ATTR_HUB]]
|
||||||
await hub.async_close()
|
await hub.async_close()
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN, SERVICE_STOP, async_stop_hub, schema=service_stop_start_schema
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_restart_hub(service: ServiceCall) -> None:
|
async def async_restart_hub(service: ServiceCall) -> None:
|
||||||
"""Restart Modbus hub."""
|
"""Restart Modbus hub."""
|
||||||
async_dispatcher_send(hass, SIGNAL_START_ENTITY)
|
async_dispatcher_send(hass, SIGNAL_START_ENTITY)
|
||||||
hub = hub_collect[service.data[ATTR_HUB]]
|
hub = hub_collect[service.data[ATTR_HUB]]
|
||||||
await hub.async_restart()
|
await hub.async_restart()
|
||||||
|
|
||||||
hass.services.async_register(
|
for x_service in (
|
||||||
DOMAIN, SERVICE_RESTART, async_restart_hub, schema=service_stop_start_schema
|
(SERVICE_STOP, async_stop_hub),
|
||||||
)
|
(SERVICE_RESTART, async_restart_hub),
|
||||||
|
):
|
||||||
|
hass.services.async_register(
|
||||||
|
DOMAIN,
|
||||||
|
x_service[0],
|
||||||
|
x_service[1],
|
||||||
|
schema=vol.Schema({vol.Required(ATTR_HUB): cv.string}),
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue