wyzeapy.services.hms_service

 1#  Copyright (c) 2021. Mulliken, LLC - All Rights Reserved
 2#  You may use, distribute and modify this code under the terms
 3#  of the attached license. You should have received a copy of
 4#  the license with this file. If not, please write to:
 5#  katie@mulliken.net to receive a copy
 6from enum import Enum
 7from typing import Optional
 8
 9from ..wyze_auth_lib import WyzeAuthLib
10from .base_service import BaseService
11
12
13class HMSMode(Enum):
14    CHANGING = 'changing'
15    DISARMED = 'disarm'
16    AWAY = 'away'
17    HOME = 'home'
18
19
20class HMSService(BaseService):
21    async def update(self, hms_id: str):
22        hms_mode = await self._monitoring_profile_state_status(hms_id)
23        return HMSMode(hms_mode['message'])
24
25    def __init__(self, auth_lib: WyzeAuthLib):
26        super().__init__(auth_lib)
27
28        self._hms_id = None
29
30    @classmethod
31    async def create(cls, auth_lib: WyzeAuthLib):
32        hms_service = cls(auth_lib)
33        hms_service._hms_id = await hms_service._get_hms_id()
34
35        return hms_service
36
37    @property
38    def hms_id(self) -> Optional[str]:
39        return self._hms_id
40
41    @property
42    async def has_hms(self):
43        if self._hms_id is None:
44            self._hms_id = self.hms_id
45
46        return self._hms_id is not None
47
48    async def set_mode(self, mode: HMSMode):
49        if mode == HMSMode.DISARMED:
50            await self._disable_reme_alarm(self.hms_id)
51            await self._monitoring_profile_active(self.hms_id, 0, 0)
52        elif mode == HMSMode.AWAY:
53            await self._monitoring_profile_active(self.hms_id, 0, 1)
54        elif mode == HMSMode.HOME:
55            await self._monitoring_profile_active(self.hms_id, 1, 0)
56
57    async def _get_hms_id(self) -> Optional[str]:
58        """
59        Processes the response from _get_plan_binding_list_by_user to get the hms_id
60
61        :return: The hms_id or nothing if there is no hms in the account
62        """
63
64        await self._auth_lib.refresh_if_should()
65
66        if self._hms_id is not None:
67            return self._hms_id
68
69        response = await self._get_plan_binding_list_by_user()
70        hms_subs = response['data']
71
72        if len(hms_subs) >= 1:
73            for sub in hms_subs:
74                if (devices := sub.get('deviceList')) is not None and len(devices) >= 1:
75                    for device in devices:
76                        self._hms_id = str(device['device_id'])
77                        return self._hms_id
78
79        return None
class HMSMode(enum.Enum):
14class HMSMode(Enum):
15    CHANGING = 'changing'
16    DISARMED = 'disarm'
17    AWAY = 'away'
18    HOME = 'home'
CHANGING = <HMSMode.CHANGING: 'changing'>
DISARMED = <HMSMode.DISARMED: 'disarm'>
AWAY = <HMSMode.AWAY: 'away'>
HOME = <HMSMode.HOME: 'home'>
class HMSService(wyzeapy.services.base_service.BaseService):
21class HMSService(BaseService):
22    async def update(self, hms_id: str):
23        hms_mode = await self._monitoring_profile_state_status(hms_id)
24        return HMSMode(hms_mode['message'])
25
26    def __init__(self, auth_lib: WyzeAuthLib):
27        super().__init__(auth_lib)
28
29        self._hms_id = None
30
31    @classmethod
32    async def create(cls, auth_lib: WyzeAuthLib):
33        hms_service = cls(auth_lib)
34        hms_service._hms_id = await hms_service._get_hms_id()
35
36        return hms_service
37
38    @property
39    def hms_id(self) -> Optional[str]:
40        return self._hms_id
41
42    @property
43    async def has_hms(self):
44        if self._hms_id is None:
45            self._hms_id = self.hms_id
46
47        return self._hms_id is not None
48
49    async def set_mode(self, mode: HMSMode):
50        if mode == HMSMode.DISARMED:
51            await self._disable_reme_alarm(self.hms_id)
52            await self._monitoring_profile_active(self.hms_id, 0, 0)
53        elif mode == HMSMode.AWAY:
54            await self._monitoring_profile_active(self.hms_id, 0, 1)
55        elif mode == HMSMode.HOME:
56            await self._monitoring_profile_active(self.hms_id, 1, 0)
57
58    async def _get_hms_id(self) -> Optional[str]:
59        """
60        Processes the response from _get_plan_binding_list_by_user to get the hms_id
61
62        :return: The hms_id or nothing if there is no hms in the account
63        """
64
65        await self._auth_lib.refresh_if_should()
66
67        if self._hms_id is not None:
68            return self._hms_id
69
70        response = await self._get_plan_binding_list_by_user()
71        hms_subs = response['data']
72
73        if len(hms_subs) >= 1:
74            for sub in hms_subs:
75                if (devices := sub.get('deviceList')) is not None and len(devices) >= 1:
76                    for device in devices:
77                        self._hms_id = str(device['device_id'])
78                        return self._hms_id
79
80        return None

Base service class for interacting with Wyze devices.

HMSService(auth_lib: wyzeapy.wyze_auth_lib.WyzeAuthLib)
26    def __init__(self, auth_lib: WyzeAuthLib):
27        super().__init__(auth_lib)
28
29        self._hms_id = None

Initialize the base service.

async def update(self, hms_id: str):
22    async def update(self, hms_id: str):
23        hms_mode = await self._monitoring_profile_state_status(hms_id)
24        return HMSMode(hms_mode['message'])
@classmethod
async def create(cls, auth_lib: wyzeapy.wyze_auth_lib.WyzeAuthLib):
31    @classmethod
32    async def create(cls, auth_lib: WyzeAuthLib):
33        hms_service = cls(auth_lib)
34        hms_service._hms_id = await hms_service._get_hms_id()
35
36        return hms_service
hms_id: Optional[str]
38    @property
39    def hms_id(self) -> Optional[str]:
40        return self._hms_id
has_hms
42    @property
43    async def has_hms(self):
44        if self._hms_id is None:
45            self._hms_id = self.hms_id
46
47        return self._hms_id is not None
async def set_mode(self, mode: HMSMode):
49    async def set_mode(self, mode: HMSMode):
50        if mode == HMSMode.DISARMED:
51            await self._disable_reme_alarm(self.hms_id)
52            await self._monitoring_profile_active(self.hms_id, 0, 0)
53        elif mode == HMSMode.AWAY:
54            await self._monitoring_profile_active(self.hms_id, 0, 1)
55        elif mode == HMSMode.HOME:
56            await self._monitoring_profile_active(self.hms_id, 1, 0)