async def websocket_get_aldb( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Get the All-Link Database for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return # Convert the ALDB to a dict merge in pending changes aldb = {mem_addr: device.aldb[mem_addr] for mem_addr in device.aldb} aldb.update(device.aldb.pending_changes) changed_records = list(device.aldb.pending_changes.keys()) dev_registry = await hass.helpers.device_registry.async_get_registry() records = [ await async_aldb_record_to_dict(dev_registry, aldb[mem_addr], mem_addr in changed_records) for mem_addr in aldb ] connection.send_result(msg[ID], records)
async def websocket_add_device( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Add one or more Insteon devices.""" @callback def linking_complete(address: str, action: DeviceAction): """Forward device events to websocket.""" if action == DeviceAction.COMPLETED: forward_data = {"type": "linking_stopped", "address": ""} else: return connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def async_cleanup() -> None: """Remove signal listeners.""" devices.unsubscribe(linking_complete) connection.subscriptions[msg["id"]] = async_cleanup devices.subscribe(linking_complete) async for address in devices.async_add_device( address=msg.get(DEVICE_ADDRESS), multiple=msg[MULTIPLE]): forward_data = {"type": "device_added", "address": str(address)} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) connection.send_result(msg[ID])
async def websocket_release_notes( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Get the full release notes for a entity.""" component = hass.data[DOMAIN] entity: UpdateEntity | None = component.get_entity(msg["entity_id"]) if entity is None: connection.send_error( msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" ) return if not entity.supported_features & UpdateEntityFeature.RELEASE_NOTES: connection.send_error( msg["id"], websocket_api.const.ERR_NOT_SUPPORTED, "Entity does not support release notes", ) return connection.send_result( msg["id"], await entity.async_release_notes(), )
async def websocket_cancel_add_device( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Cancel the Insteon all-linking process.""" await devices.async_cancel_all_linking() connection.send_result(msg[ID])
async def websocket_analytics( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Return analytics preferences.""" analytics: Analytics = hass.data[DOMAIN] connection.send_result( msg["id"], {ATTR_PREFERENCES: analytics.preferences}, )
async def websocket_change_properties_record( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Add the default All-Link Database records for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return set_property(device, msg[PROPERTY_NAME], msg[PROPERTY_VALUE]) connection.send_result(msg[ID])
async def websocket_reset_aldb( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Create an All-Link Database record for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return device.aldb.clear_pending() connection.send_result(msg[ID])
async def websocket_load_aldb( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Create an All-Link Database record for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return hass.async_create_task(async_reload_and_save_aldb(hass, device)) connection.send_result(msg[ID])
async def websocket_get_properties( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Add the default All-Link Database records for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return properties, schema = get_properties(device) connection.send_result(msg[ID], {"properties": properties, "schema": schema})
async def websocket_notify_on_aldb_status( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Tell Insteon a new ALDB record was added.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return @callback def record_added(controller, responder, group): """Forward ALDB events to websocket.""" forward_data = {"type": "record_loaded"} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def aldb_loaded(): """Forward ALDB loaded event to websocket.""" forward_data = { "type": "status_changed", "is_loading": device.aldb.status == ALDBStatus.LOADING, } connection.send_message( websocket_api.event_message(msg["id"], forward_data)) @callback def async_cleanup() -> None: """Remove signal listeners.""" unsubscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") unsubscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") unsubscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") forward_data = {"type": "unsubscribed"} connection.send_message( websocket_api.event_message(msg["id"], forward_data)) connection.subscriptions[msg["id"]] = async_cleanup subscribe_topic(record_added, f"{DEVICE_LINK_CONTROLLER_CREATED}.{device.id}") subscribe_topic(record_added, f"{DEVICE_LINK_RESPONDER_CREATED}.{device.id}") subscribe_topic(aldb_loaded, f"{device.id}.{ALDB_STATUS_CHANGED}") connection.send_result(msg[ID])
async def websocket_analytics_preferences( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Update analytics preferences.""" preferences = msg[ATTR_PREFERENCES] analytics: Analytics = hass.data[DOMAIN] await analytics.save_preferences(preferences) await analytics.send_analytics() connection.send_result( msg["id"], {ATTR_PREFERENCES: analytics.preferences}, )
async def websocket_reset_properties( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Add the default All-Link Database records for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return for prop in device.operating_flags: device.operating_flags[prop].new_value = None for prop in device.properties: device.properties[prop].new_value = None connection.send_result(msg[ID])
async def websocket_create_aldb_record( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Create an All-Link Database record for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return record = msg[ALDB_RECORD] device.aldb.add( group=record["group"], controller=record["is_controller"], target=record["target"], data1=record["data1"], data2=record["data2"], data3=record["data3"], ) connection.send_result(msg[ID])
def websocket_handle_reorder( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Handle reordering shopping_list items.""" msg_id = msg.pop("id") try: hass.data[DOMAIN].async_reorder(msg.pop("item_ids")) hass.bus.async_fire(EVENT, {"action": "reorder"}, context=connection.context(msg)) connection.send_result(msg_id) except KeyError: connection.send_error( msg_id, websocket_api.const.ERR_NOT_FOUND, "One or more item id(s) not found.", ) except vol.Invalid as err: connection.send_error(msg_id, websocket_api.const.ERR_INVALID_FORMAT, f"{err}")
async def websocket_load_properties( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Add the default All-Link Database records for an Insteon device.""" device = devices[msg[DEVICE_ADDRESS]] if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return result1 = await device.async_read_op_flags() result2 = await device.async_read_ext_properties() await devices.async_save(workdir=hass.config.config_dir) if result1 != ResponseStatus.SUCCESS or result2 != ResponseStatus.SUCCESS: connection.send_message( websocket_api.error_message( msg[ID], "load_failed", "properties not loaded from device" ) ) return connection.send_result(msg[ID])
async def websocket_get_device( hass: HomeAssistant, connection: websocket_api.connection.ActiveConnection, msg: dict, ) -> None: """Get an Insteon device.""" dev_registry = await hass.helpers.device_registry.async_get_registry() ha_device = dev_registry.async_get(msg[DEVICE_ID]) if not ha_device: notify_device_not_found(connection, msg, HA_DEVICE_NOT_FOUND) return device = get_insteon_device_from_ha_device(ha_device) if not device: notify_device_not_found(connection, msg, INSTEON_DEVICE_NOT_FOUND) return ha_name = compute_device_name(ha_device) device_info = { "name": ha_name, "address": str(device.address), "is_battery": device.is_battery, "aldb_status": str(device.aldb.status), } connection.send_result(msg[ID], device_info)