File: preset.py

package info (click to toggle)
python-asyncsleepiq 1.5.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: python: 1,047; makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Foundation preset setting from SleepIQ API."""
from __future__ import annotations

from typing import Any
from .api import SleepIQAPI
from .consts import BED_PRESETS, NO_PRESET, SIDES_FULL, SIDES_SHORT, Side


class SleepIQPreset:
    """Foundation preset setting from SleepIQ API."""

    def __init__(self, api: SleepIQAPI, bed_id: str, side: Side) -> None:
        """Initialize preset setting."""
        self._api = api
        self.bed_id = bed_id
        self.side = side
        self.side_full = SIDES_FULL[side]
        self.preset = ""
        self.options = list(BED_PRESETS)

    def __str__(self) -> str:
        """Return string representation."""
        return f"SleepIQPreset[{self.side}](preset={self.preset})"

    def __repr__(self) -> str:
        """Return string representation."""
        return f"SleepIQPreset[{self.side}](preset={self.preset})"

    async def set_preset(self, preset: str, slow_speed: bool = False) -> None:
        """Set foundation preset."""
        #
        # preset 1-6
        # slowSpeed False=fast, True=slow
        #
        if preset == NO_PRESET:
            return
        if preset not in self.options:
            raise ValueError("Invalid preset")
        data = {"preset": BED_PRESETS[preset], "side": SIDES_SHORT[self.side], "speed": 1 if slow_speed else 0}
        await self._api.put("bed/" + self.bed_id + "/foundation/preset", data)

    async def update(self, data: dict[str, Any]) -> None:
        """Update the position of an actuator from the API."""
        self.preset = data[f"fsCurrentPositionPreset{self.side_full}"]