File: v3.py

package info (click to toggle)
simplisafe-python 2024.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: python: 5,252; sh: 50; makefile: 19
file content (100 lines) | stat: -rw-r--r-- 2,865 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Define a v3 (new) SimpliSafe sensor."""
from __future__ import annotations

from typing import Any, cast

from simplipy.device import DeviceTypes, DeviceV3


class SensorV3(DeviceV3):
    """A V3 (new) sensor.

    Note that this class shouldn't be instantiated directly; it will be
    instantiated as appropriate via :meth:`simplipy.API.async_get_systems`.
    """

    @property
    def trigger_instantly(self) -> bool:
        """Return whether the sensor will trigger instantly.

        Returns:
            The "instant trigger" status.
        """
        return (
            self._system.sensor_data[self._serial]["setting"].get(
                "instantTrigger", False
            )
            is True
        )

    @property
    def triggered(self) -> bool:
        """Return whether the sensor has been triggered.

        Returns:
            The triggered status.
        """
        if self.type in (
            DeviceTypes.CARBON_MONOXIDE,
            DeviceTypes.ENTRY,
            DeviceTypes.GLASS_BREAK,
            DeviceTypes.LEAK,
            DeviceTypes.MOTION,
            DeviceTypes.MOTION_V2,
            DeviceTypes.SMOKE,
            DeviceTypes.TEMPERATURE,
        ):
            return (
                self._system.sensor_data[self._serial]["status"].get("triggered")
                is True
            )

        if self.type == DeviceTypes.SMOKE_AND_CARBON_MONOXIDE:
            return (
                self._system.sensor_data[self._serial]["status"].get(
                    "coTriggered", False
                )
                is True
                or self._system.sensor_data[self._serial]["status"].get(
                    "smokeTriggered", False
                )
                is True
            )

        return False

    @property
    def temperature(self) -> int:
        """Return the temperature of the sensor (as appropriate).

        If the sensor isn't a temperature sensor, an ``AttributeError`` will be raised.

        Returns:
            The temperature.

        Raises:
            AttributeError: Raised when property is read on a non-temperature device.
        """
        if self.type != DeviceTypes.TEMPERATURE:
            raise AttributeError("Non-temperature sensor cannot have a temperature")

        return cast(
            int, self._system.sensor_data[self._serial]["status"]["temperature"]
        )

    def as_dict(self) -> dict[str, Any]:
        """Return dictionary version of this device.

        Returns:
            A dict representation of this device.
        """
        data: dict[str, Any] = {
            **super().as_dict(),
            "trigger_instantly": self.trigger_instantly,
            "triggered": self.triggered,
        }

        if self.type == DeviceTypes.TEMPERATURE:
            data["temperature"] = self.temperature

        return data