Ejemplo n.º 1
0
async def turn_on(request: web.Request) -> web.Response:
    """Use to turn on the device."""
    if request.body_exists:
        body = await request.json()
        minutes = int(body[KEY_MINUTES]) if body.get(KEY_MINUTES) else 0
    else:
        minutes = 0
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(
            _serialize_object(await swapi.control_device(Command.ON, minutes)))
Ejemplo n.º 2
0
async def set_name(request: web.Request) -> web.Response:
    """Use to set the device's name."""
    try:
        body = await request.json()
        name = body[KEY_NAME]
    except Exception as exc:
        raise ValueError(
            f"failed to get {KEY_NAME} from body as json") from exc
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(
            _serialize_object(await swapi.set_device_name(name)))
Ejemplo n.º 3
0
async def create_schedule(request: web.Request) -> web.Response:
    """Use to create a new schedule."""
    weekdays = dict(map(lambda d: (d.value, d), Days))
    body = await request.json()
    start_time = body[KEY_START]
    stop_time = body[KEY_STOP]
    selected_days = (set([weekdays[d] for d in body[KEY_DAYS]])
                     if body.get(KEY_DAYS) else set())
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(
            _serialize_object(await
                              swapi.create_schedule(start_time, stop_time,
                                                    selected_days)))
Ejemplo n.º 4
0
async def set_auto_shutdown(request: web.Request) -> web.Response:
    """Use to set the device's auto shutdown configuration value."""
    try:
        body = await request.json()
        hours = body[KEY_HOURS]
        minutes = int(body[KEY_MINUTES]) if body.get(KEY_MINUTES) else 0
    except Exception as exc:
        raise ValueError("failed to get hours from body as json") from exc
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(
            _serialize_object(await swapi.set_auto_shutdown(
                timedelta(hours=int(hours),
                          minutes=int(minutes) if minutes else 0))))
Ejemplo n.º 5
0
    async def _async_call_api(self, api: str, *args: Any) -> None:
        """Call Switcher API."""
        _LOGGER.debug("Calling api for %s, api: '%s', args: %s", self.name,
                      api, args)
        response: SwitcherBaseResponse = None
        error = None

        try:
            async with SwitcherApi(self.coordinator.data.ip_address,
                                   self.coordinator.data.device_id) as swapi:
                response = await getattr(swapi, api)(*args)
        except (asyncio.TimeoutError, OSError, RuntimeError) as err:
            error = repr(err)

        if error or not response or not response.successful:
            _LOGGER.error(
                "Call api for %s failed, api: '%s', args: %s, response/error: %s",
                self.name,
                api,
                args,
                response or error,
            )
            self.coordinator.last_update_success = False
Ejemplo n.º 6
0
async def get_state(request: web.Request) -> web.Response:
    """Use to get the current state of the device."""
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(_serialize_object(await swapi.get_state()))
Ejemplo n.º 7
0
async def turn_off(request: web.Request) -> web.Response:
    """Use to turn on the device."""
    async with SwitcherApi(request.query[KEY_IP],
                           request.query[KEY_ID]) as swapi:
        return web.json_response(
            _serialize_object(await swapi.control_device(Command.OFF)))