Esempio n. 1
0
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])
Esempio n. 2
0
def websocket_handle_items(
    hass: HomeAssistant,
    connection: websocket_api.connection.ActiveConnection,
    msg: dict,
) -> None:
    """Handle get shopping_list items."""
    connection.send_message(
        websocket_api.result_message(msg["id"], hass.data[DOMAIN].items))
Esempio n. 3
0
async def websocket_handle_clear(
    hass: HomeAssistant,
    connection: websocket_api.connection.ActiveConnection,
    msg: dict,
) -> None:
    """Handle clearing shopping_list items."""
    await hass.data[DOMAIN].async_clear_completed()
    hass.bus.async_fire(EVENT, {"action": "clear"},
                        context=connection.context(msg))
    connection.send_message(websocket_api.result_message(msg["id"]))
Esempio n. 4
0
async def websocket_handle_add(
    hass: HomeAssistant,
    connection: websocket_api.connection.ActiveConnection,
    msg: dict,
) -> None:
    """Handle add item to shopping_list."""
    item = await hass.data[DOMAIN].async_add(msg["name"])
    hass.bus.async_fire(EVENT, {
        "action": "add",
        "item": item
    },
                        context=connection.context(msg))
    connection.send_message(websocket_api.result_message(msg["id"], item))
Esempio n. 5
0
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])
Esempio n. 6
0
async def websocket_handle_update(
    hass: HomeAssistant,
    connection: websocket_api.connection.ActiveConnection,
    msg: dict,
) -> None:
    """Handle update shopping_list item."""
    msg_id = msg.pop("id")
    item_id = msg.pop("item_id")
    msg.pop("type")
    data = msg

    try:
        item = await hass.data[DOMAIN].async_update(item_id, data)
        hass.bus.async_fire(EVENT, {
            "action": "update",
            "item": item
        },
                            context=connection.context(msg))
        connection.send_message(websocket_api.result_message(msg_id, item))
    except KeyError:
        connection.send_message(
            websocket_api.error_message(msg_id, "item_not_found",
                                        "Item not found"))