def ws_send(self, command, **kwargs): if self._ws_connection: self._ws_connection.send_message( event_message(self._ws_cid, { "command": command, **kwargs, }))
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])
def device_registered(device: DeviceEntry) -> None: device_details = {"name": device.name, "id": device.id} connection.send_message( websocket_api.event_message( msg[ID], {"event": "device registered", "device": device_details} ) )
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))
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))
async def forward_messages(mqttmsg: Message): """Forward events to websocket.""" connection.send_message(websocket_api.event_message(msg['id'], { 'topic': mqttmsg.topic, 'payload': mqttmsg.payload, 'qos': mqttmsg.qos, 'retain': mqttmsg.retain, }))
def send(self, command, **kwargs): if self.connection: connection, cid = self.connection[-1] connection.send_message( event_message(cid, { "command": command, **kwargs, }))
def log_config_updates(event: dict) -> None: log_config: LogConfig = event["log_config"] connection.send_message( websocket_api.event_message( msg[ID], { "type": "log_config", "log_config": dataclasses.asdict(log_config), }, ))
def forward_progress(event: dict) -> None: progress: FirmwareUpdateProgress = event["firmware_update_progress"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], **_get_firmware_update_progress_dict(progress), }, ))
def forward_node(node): """Forward node events to websocket.""" if node.node_id != msg[NODE_ID]: return forward_data = { "type": "node_updated", ATTR_NODE_QUERY_STAGE: node.node_query_stage, } connection.send_message(websocket_api.event_message(msg["id"], forward_data))
def forward_progress(event: dict) -> None: progress: FirmwareUpdateProgress = event["firmware_update_progress"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], "sent_fragments": progress.sent_fragments, "total_fragments": progress.total_fragments, }, ))
def forward_trigger(event, context=None): """Forward events to websocket.""" message = event_message( self._message_id, { "type": EVENT_DEVICE_TRIGGER, "data": event["trigger"] }, ) self._connection.send_message( json.dumps(message, cls=NodeRedJSONEncoder, allow_nan=False))
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))
def breakpoint_hit(automation_id, run_id, node): """Forward events to websocket.""" connection.send_message( websocket_api.event_message( msg["id"], { "automation_id": automation_id, "run_id": run_id, "node": node, }, ))
def node_removed(event: dict) -> None: node = event["node"] node_details = { "node_id": node.node_id, } connection.send_message( websocket_api.event_message(msg[ID], { "event": "node removed", "node": node_details }))
def forward_finished(event: dict) -> None: finished: FirmwareUpdateFinished = event["firmware_update_finished"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], "status": finished.status, "wait_time": finished.wait_time, }, ))
def forward_stats(event: dict) -> None: statistics: ControllerStatistics = event["statistics_updated"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], "source": "controller", **_get_controller_statistics_dict(statistics), }, ))
def press(self) -> None: """Handle the button press.""" self._connection.send_message( event_message( self._message_id, { CONF_TYPE: EVENT_TRIGGER_NODE, "data": { "entity": self.hass.states.get(self.entity_id), }, }, ))
def forward_stats(event: dict) -> None: statistics: NodeStatistics = event["statistics_updated"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], "source": "node", "node_id": node.node_id, **_get_node_statistics_dict(statistics), }, ))
def forward_event(event: dict) -> None: log_msg: LogMessage = event["log_message"] connection.send_message( websocket_api.event_message( msg[ID], { "timestamp": log_msg.timestamp, "level": log_msg.level, "primary_tags": log_msg.primary_tags, "message": log_msg.formatted_message, }, ))
async def forward_messages(mqttmsg: Message): """Forward events to websocket.""" connection.send_message( websocket_api.event_message( msg["id"], { "topic": mqttmsg.topic, "payload": mqttmsg.payload, "qos": mqttmsg.qos, "retain": mqttmsg.retain, }, ))
async def handle_webhook(hass, id, request): """Handle webhook callback.""" body = await request.text() try: payload = json.loads(body) if body else {} except ValueError: payload = body data = {"payload": payload, "headers": dict(request.headers)} _LOGGER.debug(f"Webhook received {id[:15]}..: {data}") connection.send_message(event_message(msg[CONF_ID], {"data": data}))
def node_added(event: dict) -> None: node = event["node"] node_details = { "node_id": node.node_id, "status": node.status, "ready": node.ready, } connection.send_message( websocket_api.event_message( msg[ID], {"event": "node added", "node": node_details} ) )
def breakpoint_hit(key, run_id, node): """Forward events to websocket.""" connection.send_message( websocket_api.event_message( msg["id"], { "domain": key[0], "item_id": key[1], "run_id": run_id, "node": node, }, ))
def breakpoint_hit(key, run_id, node): """Forward events to websocket.""" domain, item_id = key.split(".", 1) connection.send_message( websocket_api.event_message( msg["id"], { "domain": domain, "item_id": item_id, "run_id": run_id, "node": node, }, ))
async def async_trigger_node(self, **kwargs) -> None: """Trigger node in Node-RED.""" data = {} data[CONF_ENTITY_ID] = kwargs.get(CONF_TRIGGER_ENTITY_ID) data[CONF_SKIP_CONDITION] = kwargs.get(CONF_SKIP_CONDITION, False) data[CONF_OUTPUT_PATH] = kwargs.get(CONF_OUTPUT_PATH, True) self._connection.send_message( event_message( self._message_id, {CONF_TYPE: EVENT_AUTOMATION_TRIGGERED, CONF_DATA: data}, ) )
def node_removed(event: dict) -> None: node = event["node"] node_details = { "node_id": node.node_id, } # Remove from device registry hass.async_create_task(remove_from_device_registry(hass, client, node)) connection.send_message( websocket_api.event_message( msg[ID], {"event": "node removed", "node": node_details} ) )
def forward_event(event: MassEvent) -> None: if isinstance(event.data, list): event_data = [x.to_dict() for x in event.data] elif event.data and hasattr(event.data, "to_dict"): event_data = event.data.to_dict() else: event_data = event.data connection.send_message( websocket_api.event_message( msg[ID], { "event": event.type.value, "object_id": event.object_id, "data": event_data, }, ))
def node_added(event: dict) -> None: node = event["node"] interview_unsubs = [ node.on("interview started", forward_event), node.on("interview completed", forward_event), node.on("interview stage completed", forward_stage), node.on("interview failed", forward_event), ] unsubs.extend(interview_unsubs) node_details = { "node_id": node.node_id, "status": node.status, "ready": node.ready, } connection.send_message( websocket_api.event_message(msg[ID], { "event": "node added", "node": node_details }))
async def forward_messages(mqttmsg: ReceiveMessage): """Forward events to websocket.""" try: payload = cast(bytes, mqttmsg.payload).decode( DEFAULT_ENCODING) # not str because encoding is set to None except (AttributeError, UnicodeDecodeError): # Convert non UTF-8 payload to a string presentation payload = str(mqttmsg.payload) connection.send_message( websocket_api.event_message( msg["id"], { "topic": mqttmsg.topic, "payload": payload, "qos": mqttmsg.qos, "retain": mqttmsg.retain, }, ))
async def forward_messages(data): """Forward events to websocket.""" connection.send_message(websocket_api.event_message(msg['id'], data))