Skip to content

Commit

Permalink
Update models for updated schema and new resource types (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt authored Sep 25, 2023
1 parent 25b4391 commit 541e232
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 25 deletions.
8 changes: 7 additions & 1 deletion aiohue/v2/controllers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..models.matter_fabric import MatterFabric
from ..models.resource import ResourceTypes
from ..models.behavior_script import BehaviorScript
from ..models.behavior_instance import BehaviorInstance
from ..models.behavior_instance import BehaviorInstance, BehaviorInstancePut
from .base import BaseResourcesController, GroupedControllerBase

if TYPE_CHECKING:
Expand Down Expand Up @@ -100,6 +100,10 @@ class BehaviorInstanceController(BaseResourcesController[Type[BehaviorInstance]]
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 ConfigController(
GroupedControllerBase[
Expand Down Expand Up @@ -183,6 +187,7 @@ def __init__(self, bridge: "HueBridgeV2") -> None:
self.matter = MatterController(bridge)
self.matter_fabric = MatterFabricController(bridge)
self.behavior_script = BehaviorScriptController(bridge)
self.behavior_instance = BehaviorInstanceController(bridge)
super().__init__(
bridge,
[
Expand All @@ -194,5 +199,6 @@ def __init__(self, bridge: "HueBridgeV2") -> None:
self.matter,
self.matter_fabric,
self.behavior_script,
self.behavior_instance,
],
)
48 changes: 48 additions & 0 deletions aiohue/v2/controllers/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from aiohue.util import dataclass_to_dict

from ..models.button import Button, ButtonEvent
from ..models.camera_motion import CameraMotion, CameraMotionPut
from ..models.contact import Contact, ContactPut
from ..models.relative_rotary import RelativeRotary
from ..models.device_power import DevicePower
from ..models.geofence_client import GeofenceClient
from ..models.light_level import LightLevel, LightLevelPut
from ..models.motion import Motion, MotionPut
from ..models.resource import ResourceTypes
from ..models.tamper import Tamper
from ..models.temperature import Temperature
from ..models.zigbee_connectivity import ZigbeeConnectivity
from .base import BaseResourcesController, GroupedControllerBase
Expand All @@ -22,10 +25,13 @@
SENSOR_TYPES = Union[
DevicePower,
Button,
CameraMotion,
Contact,
GeofenceClient,
LightLevel,
Motion,
RelativeRotary,
Tamper,
Temperature,
ZigbeeConnectivity,
]
Expand Down Expand Up @@ -115,6 +121,34 @@ async def _handle_longpress_workaround(self, id: int):
self._logger.debug("Long press workaround for FOH switch completed.")


class CameraMotionController(BaseResourcesController[Type[CameraMotion]]):
"""Controller holding and managing HUE resources of type `camera_motion`."""

item_type = ResourceTypes.CAMERA_MOTION
item_cls = CameraMotion
allow_parser_error = True

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

async def set_sensitivity(self, id: str, sensitivity: int) -> None:
"""Enable/Disable sensor."""
await self.update(id, CameraMotionPut(sensitivity=sensitivity))


class ContactController(BaseResourcesController[Type[Contact]]):
"""Controller holding and managing HUE resources of type `contact`."""

item_type = ResourceTypes.CONTACT
item_cls = Contact
allow_parser_error = True

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


class GeofenceClientController(BaseResourcesController[Type[GeofenceClient]]):
"""Controller holding and managing HUE resources of type `geofence_client`."""

Expand Down Expand Up @@ -155,6 +189,14 @@ class RelativeRotaryController(BaseResourcesController[Type[Button]]):
allow_parser_error = True


class TamperController(BaseResourcesController[Type[Tamper]]):
"""Controller holding and managing HUE resources of type `tamper`."""

item_type = ResourceTypes.TAMPER
item_cls = Tamper
allow_parser_error = True


class TemperatureController(BaseResourcesController[Type[Temperature]]):
"""Controller holding and managing HUE resources of type `temperature`."""

Expand All @@ -177,22 +219,28 @@ class SensorsController(GroupedControllerBase[SENSOR_TYPES]):
def __init__(self, bridge: "HueBridgeV2") -> None:
"""Initialize instance."""
self.button = ButtonController(bridge)
self.camera_motion = CameraMotionController(bridge)
self.contact = ContactController(bridge)
self.device_power = DevicePowerController(bridge)
self.geofence_client = GeofenceClientController(bridge)
self.light_level = LightLevelController(bridge)
self.motion = MotionController(bridge)
self.relative_rotary = RelativeRotaryController(bridge)
self.tamper = TamperController(bridge)
self.temperature = TemperatureController(bridge)
self.zigbee_connectivity = ZigbeeConnectivityController(bridge)
super().__init__(
bridge,
[
self.button,
self.camera_motion,
self.contact,
self.device_power,
self.geofence_client,
self.light_level,
self.motion,
self.relative_rotary,
self.tamper,
self.temperature,
self.zigbee_connectivity,
],
Expand Down
30 changes: 28 additions & 2 deletions aiohue/v2/models/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_button
"""
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Optional, Type
from typing import List, Optional, Type

from .resource import ResourceIdentifier, ResourceTypes

Expand All @@ -27,11 +28,36 @@ def _missing_(cls: Type, value: object):
return ButtonEvent.UNKNOWN


@dataclass
class ButtonReport:
"""
Represent ButtonReport as retrieved from api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_button_get
"""

updated: datetime
event: ButtonEvent


@dataclass
class ButtonFeature:
"""Represent ButtonFeature object as used by the Hue api."""

last_event: ButtonEvent
button_report: Optional[ButtonReport] = None
last_event: Optional[ButtonEvent] = None # deprecated
repeat_interval: Optional[int] = None
event_values: Optional[List[ButtonEvent]] = None

@property
def value(self) -> ButtonEvent:
"""Return the actual/current value."""
# prefer new style attribute (not available on older firmware versions)
if self.button_report is not None:
return self.button_report.event
if self.last_event is not None:
return self.last_event
return ButtonEvent.UNKNOWN


@dataclass
Expand Down
45 changes: 45 additions & 0 deletions aiohue/v2/models/camera_motion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Model(s) for camera_motion resource on HUE bridge.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_camera_motion
"""
from dataclasses import dataclass
from typing import Optional
from .feature import (
MotionSensingFeature,
MotionSensingFeatureSensitivity,
MotionSensingFeatureSensitivityPut,
)
from .resource import ResourceIdentifier, ResourceTypes


@dataclass
class CameraMotion:
"""
Represent a (full) `CameraMotion` resource when retrieved from the api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_camera_motion_get
"""

id: str
owner: ResourceIdentifier
# enabled: required(boolean)
# true when sensor is activated, false when deactivated
enabled: bool
motion: MotionSensingFeature
sensitivity: Optional[MotionSensingFeatureSensitivity]

id_v1: Optional[str] = None
type: ResourceTypes = ResourceTypes.CAMERA_MOTION


@dataclass
class CameraMotionPut:
"""
CameraMotion resource properties that can be set/updated with a PUT request.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_camera_motion__id__put
"""

enabled: Optional[bool] = None
sensitivity: Optional[MotionSensingFeatureSensitivityPut] = None
60 changes: 60 additions & 0 deletions aiohue/v2/models/contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Model(s) for contact resource on HUE bridge.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_contact
"""
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Optional

from .resource import ResourceIdentifier, ResourceTypes


class ContactState(Enum):
"""State of a Contact sensor."""

CONTACT = "contact"
NO_CONTACT = "no_contact"


@dataclass
class ContactReport:
"""
Represent ContactReport as retrieved from api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_contact_get
"""

changed: datetime
state: ContactState


@dataclass
class Contact:
"""
Represent a (full) `Contact` resource when retrieved from the api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_contact_get
"""

id: str
owner: ResourceIdentifier
# enabled: required(boolean)
# true when sensor is activated, false when deactivated
enabled: bool
contact_report: Optional[ContactReport] = None

id_v1: Optional[str] = None
type: ResourceTypes = ResourceTypes.CONTACT


@dataclass
class ContactPut:
"""
Contact resource properties that can be set/updated with a PUT request.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_contact__id__put
"""

enabled: Optional[bool] = None
Loading

0 comments on commit 541e232

Please sign in to comment.