Debian Codegraph @

blebox_uniapi/button.py

raw symbols SCIP
1 from .cover import GateBoxControlType, GateBoxExtraButtonType
2 from .feature import Feature
3 from typing import TYPE_CHECKING, Any, Optional, Type
4
5 from enum import Enum, auto
6
7 if TYPE_CHECKING:
8 from .box import Box
9
10 TV_LIFT_CONTROL_TYPES_API = {
11 0: {"1": "open_or_stop", "2": "close_or_stop"},
12 1: {"1": "up_or_stop", "2": "down_or_stop"},
13 2: {"1": "up_or_stop", "2": "down_or_stop"},
14 3: {"1": "up_or_stop", "2": "down_or_stop"},
15 4: {"1": "open_or_stop", "2": "close_or_stop", "3": "to_fav"},
16 }
17
18
19 class ControlType(Enum):
20 UP = auto()
21 DOWN = auto()
22 FAVORITE = auto()
23 OPEN = auto()
24 CLOSE = auto()
25
26
27 class Buttons:
28 """Buttons defines how a product derives its buttons from extended state."""
29
30 @staticmethod
31 def many_from_extended_state(
32 button_cls: Type["Button"],
33 product: "Box",
34 alias: str,
35 methods: dict,
36 extended_state: dict,
37 ) -> list["Button"]:
38 raise NotImplementedError # pragma: no cover
39
40
41 class TvLift(Buttons):
42 @staticmethod
43 def many_from_extended_state(
44 button_cls: Type["Button"],
45 product: "Box",
46 alias: str,
47 methods: dict,
48 extended_state: dict,
49 ) -> list["Button"]:
50 control_type = extended_state.get("tvLift", {}).get("controlType")
51 endpoints = TV_LIFT_CONTROL_TYPES_API.get(control_type, {})
52 return [
53 button_cls(product, f"{alias}_{endpoint}", methods, endpoint)
54 for endpoint in endpoints.values()
55 ]
56
57
58 class GateBoxSecondOutput(Buttons):
59 """GateBoxSecondOutput exposes the gateBox extra button output as a button.
60
61 WALK_IN and OTHER share one button name because what the output does is a
62 device setting the user can change at any time, not a hardware trait.
63 """
64
65 @staticmethod
66 def many_from_extended_state(
67 button_cls: Type["Button"],
68 product: "Box",
69 alias: str,
70 methods: dict,
71 extended_state: dict,
72 ) -> list["Button"]:
73 gate = extended_state.get("gate", {})
74 if gate.get("extraButtonType") not in (
75 GateBoxExtraButtonType.WALK_IN,
76 GateBoxExtraButtonType.OTHER,
77 ):
78 return []
79
80 # Api levels below 20230102 report no mode and cannot be wired that way.
81 if gate.get("openCloseMode") == GateBoxControlType.OPEN_CLOSE:
82 return []
83
84 return [button_cls(product, alias, methods, "second_output", "secondary")]
85
86
87 class Button(Feature):
88 def __init__(
89 self,
90 product: "Box",
91 alias: str,
92 methods: dict,
93 query_string: str,
94 api_command: str = "set",
95 ) -> None:
96 super().__init__(product, alias, methods)
97 self._device_class = "UPDATE"
98 self._query_string: str = query_string
99 self._api_command: str = api_command
100
101 @classmethod
102 def many_from_config(
103 cls, product: "Box", box_type_config: list, extended_state: Any
104 ) -> list["Button"]:
105 if not isinstance(extended_state, dict):
106 return []
107
108 features: list[Button] = []
109 for alias, methods, buttons in box_type_config:
110 features.extend(
111 buttons.many_from_extended_state(
112 cls, product, alias, methods, extended_state
113 )
114 )
115 return features
116
117 async def set(self) -> None:
118 await self.async_api_command(self._api_command, self.query_string)
119
120 def after_update(self) -> None:
121 pass
122
123 @property
124 def control_type(self) -> Optional[ControlType]:
125 """Return icon for endpoint."""
126 if "up" in self.query_string:
127 return ControlType.UP
128 elif "down" in self.query_string:
129 return ControlType.DOWN
130 elif "fav" in self.query_string:
131 return ControlType.FAVORITE
132 elif "open" in self.query_string:
133 return ControlType.OPEN
134 elif "close" in self.query_string:
135 return ControlType.CLOSE
136 else:
137 return None
138
139 @property
140 def query_string(self) -> str:
141 return self._query_string