File: config.py

package info (click to toggle)
python-aiohue 4.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 612 kB
  • sloc: python: 4,509; sh: 30; makefile: 5
file content (276 lines) | stat: -rw-r--r-- 10,248 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Controller holding and managing HUE resources that are of the config type."""

from typing import TYPE_CHECKING

from awesomeversion import AwesomeVersion

from aiohue.errors import BridgeSoftwareOutdated
from aiohue.util import mac_from_bridge_id
from aiohue.v2.models.behavior_instance import BehaviorInstance, BehaviorInstancePut
from aiohue.v2.models.behavior_script import BehaviorScript
from aiohue.v2.models.bridge import Bridge
from aiohue.v2.models.bridge_home import BridgeHome
from aiohue.v2.models.convenience_area_motion import ConvenienceAreaMotion
from aiohue.v2.models.device import Device
from aiohue.v2.models.entertainment import Entertainment
from aiohue.v2.models.entertainment_configuration import EntertainmentConfiguration
from aiohue.v2.models.homekit import Homekit
from aiohue.v2.models.matter import Matter
from aiohue.v2.models.matter_fabric import MatterFabric
from aiohue.v2.models.motion_area_configuration import (
    MotionAreaConfiguration,
    MotionAreaConfigurationPut,
)
from aiohue.v2.models.resource import ResourceTypes
from aiohue.v2.models.room import Room
from aiohue.v2.models.security_area_motion import SecurityAreaMotion
from aiohue.v2.models.service_group import (
    ServiceGroup,
    ServiceGroupMetadata,
    ServiceGroupPut,
)
from aiohue.v2.models.zone import Zone

from .base import BaseResourcesController, GroupedControllerBase

if TYPE_CHECKING:
    from aiohue.v2 import HueBridgeV2


class BridgeController(BaseResourcesController[type[Bridge]]):
    """Controller holding and managing HUE resources of type `bridge`."""

    item_type = ResourceTypes.BRIDGE
    item_cls = Bridge

    @property
    def id(self) -> str | None:
        """Return id of the only/first bridge found in items."""
        for item in self.items:
            return item.id
        return None


class BridgeHomeController(BaseResourcesController[type[BridgeHome]]):
    """Controller holding and managing HUE resources of type `bridge_home`."""

    item_type = ResourceTypes.BRIDGE_HOME
    item_cls = BridgeHome


class EntertainmentController(BaseResourcesController[type[Entertainment]]):
    """Controller holding and managing HUE resources of type `entertainment`."""

    item_type = ResourceTypes.ENTERTAINMENT
    item_cls = Entertainment
    allow_parser_error = True


class EntertainmentConfigurationController(
    BaseResourcesController[type[EntertainmentConfiguration]]
):
    """Controller holding and managing HUE resources of type `entertainment_configuration`."""

    item_type = ResourceTypes.ENTERTAINMENT_CONFIGURATION
    item_cls = EntertainmentConfiguration
    allow_parser_error = True


class HomekitController(BaseResourcesController[type[Homekit]]):
    """Controller holding and managing HUE resources of type `homekit`."""

    item_type = ResourceTypes.HOMEKIT
    item_cls = Homekit
    allow_parser_error = True


class MatterController(BaseResourcesController[type[Matter]]):
    """Controller holding and managing HUE resources of type `matter`."""

    item_type = ResourceTypes.MATTER
    item_cls = Matter
    allow_parser_error = True


class MatterFabricController(BaseResourcesController[type[MatterFabric]]):
    """Controller holding and managing HUE resources of type `matter_fabric`."""

    item_type = ResourceTypes.MATTER_FABRIC
    item_cls = MatterFabric
    allow_parser_error = True


class BehaviorScriptController(BaseResourcesController[type[BehaviorScript]]):
    """Controller holding and managing HUE resources of type `behavior_script`."""

    item_type = ResourceTypes.BEHAVIOR_SCRIPT
    item_cls = BehaviorScript
    allow_parser_error = True


class BehaviorInstanceController(BaseResourcesController[type[BehaviorInstance]]):
    """Controller holding and managing HUE resources of type `behavior_instance`."""

    item_type = ResourceTypes.BEHAVIOR_INSTANCE
    item_cls = BehaviorInstance
    allow_parser_error = True

    async def set_enabled(self, id: str, enabled: bool) -> None:
        """Enable/Disable sensor."""
        await self.update(id, BehaviorInstancePut(enabled=enabled))


class MotionAreaConfigurationController(
    BaseResourcesController[type[MotionAreaConfiguration]]
):
    """Controller holding and managing HUE resources of type `motion_area_configuration`."""

    item_type = ResourceTypes.MOTION_AREA_CONFIGURATION
    item_cls = MotionAreaConfiguration
    allow_parser_error = True

    def get_sensors(self, id: str) -> list[ConvenienceAreaMotion | SecurityAreaMotion]:
        """Return all sensors in given motion area configuration."""
        return [
            x
            for x in self._bridge.sensors
            if x.owner.rid == id
            and isinstance(x, (ConvenienceAreaMotion, SecurityAreaMotion))
        ]

    def get_group(self, id: str) -> Room | Zone:
        """Return the group associated with this motion area configuration."""
        if id not in self._items:
            raise KeyError(f"Motion area configuration {id} not found")
        group_id = self._items[id].group.rid
        if group_id not in self._bridge.groups:
            raise KeyError(f"Group {group_id} not found")
        return self._bridge.groups[group_id]

    async def set_enabled(self, id: str, enabled: bool) -> None:
        """Enable/Disable motion area configuration."""
        await self.update(id, MotionAreaConfigurationPut(enabled=enabled))

    async def set_name(self, id: str, name: str) -> None:
        """Set name of motion area configuration."""
        await self.update(id, MotionAreaConfigurationPut(name=name))


class ServiceGroupController(BaseResourcesController[type[ServiceGroup]]):
    """Controller holding and managing HUE resources of type `service_group`."""

    item_type = ResourceTypes.SERVICE_GROUP
    item_cls = ServiceGroup
    allow_parser_error = True

    async def set_name(self, id: str, name: str) -> None:
        """Set name of service group."""
        await self.update(id, ServiceGroupPut(metadata=ServiceGroupMetadata(name=name)))


class ConfigController(
    GroupedControllerBase[
        Bridge
        | BridgeHome
        | Entertainment
        | EntertainmentConfiguration
        | MotionAreaConfiguration
        | ServiceGroup
    ]
):
    """
    Controller holding and managing HUE resources there are of the config type.

    Note that the properties will raise AttributeError if not connected to a bridge.
    """

    @property
    def bridge_id(self) -> str:
        """Return bridge_id of bridge we're connected to."""
        return self.bridge.bridge_id

    @property
    def name(self) -> str:
        """Return name of bridge we're connected to."""
        return self.bridge_device.metadata.name

    @property
    def mac_address(self) -> str:
        """Return mac address of bridge we're connected to."""
        # the network mac is not available in api so we parse it from the id
        return mac_from_bridge_id(self.bridge_id)

    @property
    def model_id(self) -> str:
        """Return model ID of bridge we're connected to."""
        return self.bridge_device.product_data.model_id

    @property
    def software_version(self) -> str:
        """Return software version of bridge we're connected to."""
        return self.bridge_device.product_data.software_version

    @property
    def bridge(self) -> Bridge:
        """Return the only/first bridge found in items of resource `bridge`."""
        # the Hue resource system in V2 is generic and even the bridge object is returned as array
        # there should be only one object returned here
        return next(item for item in self.bridges)

    @property
    def bridge_device(self) -> Device:
        """Return the device object belonging to the bridge."""
        # the Hue resource system in V2 is generic and even the bridge metadata
        # can (only) be retrieved as a device object
        # do the plumbing by looking it up here.
        # this device object contains the metadata that is more or like comparable
        # with the V1 output like it's name and software version.
        if bridge := self.bridge:
            for device in self._bridge.devices:
                for service in device.services:
                    if service.rid == bridge.id:
                        return device
        raise AttributeError("bridge_device")

    def check_version(self, version: str) -> bool:
        """Check if bridge version is equal to (or higher than) given version."""
        current = AwesomeVersion(self.software_version)
        required = AwesomeVersion(version)
        return current >= required

    def require_version(self, version: str) -> None:
        """Raise exception if Bridge version is lower than given minimal version."""
        if not self.check_version(version):
            raise BridgeSoftwareOutdated(
                f"Bridge software version outdated. Minimal required version is {version}"
            )

    def __init__(self, bridge: "HueBridgeV2") -> None:
        """Initialize underlying controller instances."""
        self.bridges = BridgeController(bridge)
        self.bridge_home = BridgeHomeController(bridge)
        self.entertainment = EntertainmentController(bridge)
        self.entertainment_configuration = EntertainmentConfigurationController(bridge)
        self.homekit = HomekitController(bridge)
        self.matter = MatterController(bridge)
        self.matter_fabric = MatterFabricController(bridge)
        self.behavior_script = BehaviorScriptController(bridge)
        self.behavior_instance = BehaviorInstanceController(bridge)
        self.motion_area_configuration = MotionAreaConfigurationController(bridge)
        self.service_group = ServiceGroupController(bridge)
        super().__init__(
            bridge,
            [
                self.bridges,
                self.bridge_home,
                self.entertainment,
                self.entertainment_configuration,
                self.homekit,
                self.matter,
                self.matter_fabric,
                self.behavior_script,
                self.behavior_instance,
                self.motion_area_configuration,
                self.service_group,
            ],
        )