1 """API interface to get an Insteon device."""
5 from pyinsteon
import devices
6 from pyinsteon.address
import Address
7 from pyinsteon.constants
import DeviceAction
8 import voluptuous
as vol
21 INSTEON_DEVICE_NOT_FOUND,
23 SIGNAL_REMOVE_HA_DEVICE,
24 SIGNAL_REMOVE_INSTEON_DEVICE,
25 SIGNAL_REMOVE_X10_DEVICE,
28 from ..schemas
import build_x10_schema
29 from ..utils
import compute_device_name
30 from .config
import add_x10_device, remove_device_override, remove_x10_device
32 X10_DEVICE =
"x10_device"
34 REMOVE_ALL_REFS =
"remove_all_refs"
38 """Add one or more Insteon devices."""
39 async
for _
in devices.async_add_device(address=address, multiple=multiple):
44 """Return the Insteon device from an HA device."""
45 for identifier
in ha_device.identifiers:
46 if len(identifier) > 1
and identifier[0] == DOMAIN
and devices[identifier[1]]:
47 return devices[identifier[1]]
52 """Notify the caller that the device was not found."""
53 connection.send_message(
54 websocket_api.error_message(msg[ID], websocket_api.const.ERR_NOT_FOUND, text)
58 @websocket_api.websocket_command(
{vol.Required(TYPE):
"insteon/device/get", vol.Required(DEVICE_ID): str}
60 @websocket_api.require_admin
61 @websocket_api.async_response
67 """Get an Insteon device."""
68 dev_registry = dr.async_get(hass)
69 if not (ha_device := dev_registry.async_get(msg[DEVICE_ID])):
78 "address":
str(device.address),
79 "is_battery": device.is_battery,
80 "aldb_status":
str(device.aldb.status),
82 connection.send_result(msg[ID], device_info)
85 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/device/add",
86 vol.Required(MULTIPLE): bool,
87 vol.Optional(DEVICE_ADDRESS): str,
90 @websocket_api.require_admin
91 @websocket_api.async_response
97 """Add one or more Insteon devices."""
100 def linking_complete(address: str, action: DeviceAction):
101 """Forward device events to websocket."""
102 if action == DeviceAction.COMPLETED:
103 forward_data = {
"type":
"linking_stopped",
"address":
""}
106 connection.send_message(websocket_api.event_message(msg[
"id"], forward_data))
110 """Remove signal listeners."""
111 devices.unsubscribe(linking_complete)
113 connection.subscriptions[msg[
"id"]] = async_cleanup
114 devices.subscribe(linking_complete)
116 async
for address
in devices.async_add_device(
117 address=msg.get(DEVICE_ADDRESS), multiple=msg[MULTIPLE]
119 forward_data = {
"type":
"device_added",
"address":
str(address)}
120 connection.send_message(websocket_api.event_message(msg[
"id"], forward_data))
122 connection.send_result(msg[ID])
125 @websocket_api.websocket_command({vol.Required(TYPE):
"insteon/device/add/cancel"})
126 @websocket_api.require_admin
127 @websocket_api.async_response
133 """Cancel the Insteon all-linking process."""
134 await devices.async_cancel_all_linking()
135 connection.send_result(msg[ID])
138 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/device/remove",
139 vol.Required(DEVICE_ADDRESS): str,
140 vol.Required(REMOVE_ALL_REFS): bool,
143 @websocket_api.require_admin
144 @websocket_api.async_response
150 """Remove an Insteon device."""
152 address = msg[DEVICE_ADDRESS]
153 remove_all_refs = msg[REMOVE_ALL_REFS]
154 if address.startswith(
"X10"):
155 _, housecode, unitcode = address.split(
".")
156 unitcode =
int(unitcode)
160 address = Address(address)
164 hass, SIGNAL_REMOVE_INSTEON_DEVICE, address, remove_all_refs
167 connection.send_result(msg[ID])
170 @websocket_api.websocket_command(
{
vol.Required(TYPE):
"insteon/device/add_x10",
171 vol.Required(X10_DEVICE): X10_DEVICE_SCHEMA,
174 @websocket_api.require_admin
175 @websocket_api.async_response
181 """Get the schema for the X10 devices configuration."""
182 x10_device = msg[X10_DEVICE]
186 connection.send_error(msg[ID], code=
"duplicate", message=
"Duplicate X10 device")
189 connection.send_result(msg[ID])
190
def remove_x10_device(HomeAssistant hass, str housecode, int unitcode)
def remove_device_override(HomeAssistant hass, Address address)
def add_x10_device(HomeAssistant hass, X10DeviceConfig x10_device)
None websocket_cancel_add_device(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
None websocket_add_x10_device(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def async_add_devices(address, multiple)
def get_insteon_device_from_ha_device(ha_device)
None websocket_add_device(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
None websocket_get_device(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def notify_device_not_found(connection, msg, text)
None websocket_remove_device(HomeAssistant hass, websocket_api.connection.ActiveConnection connection, dict[str, Any] msg)
def build_x10_schema(housecode=vol.UNDEFINED, unitcode=vol.UNDEFINED, platform=vol.UNDEFINED, dim_steps=22)
str compute_device_name(ha_device)
None async_cleanup(HomeAssistant hass, DeviceRegistry dev_reg, entity_registry.EntityRegistry ent_reg)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)