wyzeapy.services.wall_switch_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
  6import logging
  7from enum import Enum
  8from typing import Any, Dict, List
  9
 10from .base_service import BaseService
 11from ..types import Device, WallSwitchProps, DeviceTypes
 12
 13_LOGGER = logging.getLogger(__name__)
 14
 15
 16class SinglePressType(Enum):
 17    CLASSIC = 1
 18    IOT = 2
 19
 20
 21class WallSwitch(Device):
 22    def __init__(self, dictionary: Dict[Any, Any]):
 23        super().__init__(dictionary)
 24
 25        self.switch_power: bool = False
 26        self.switch_iot: bool = False
 27        self.single_press_type: SinglePressType = SinglePressType.CLASSIC
 28
 29    @property
 30    def on(self):
 31        if self.single_press_type == SinglePressType.IOT:
 32            return self.switch_iot
 33        return self.switch_power
 34
 35    @on.setter
 36    def on(self, state: bool):
 37        if self.single_press_type == SinglePressType.IOT:
 38            self.switch_iot = state
 39        self.switch_power = state
 40
 41
 42class WallSwitchService(BaseService):
 43    async def update(self, switch: WallSwitch) -> WallSwitch:
 44        properties = (await self._wall_switch_get_iot_prop(switch))['data']['props']
 45
 46        device_props = []
 47        for prop_key, prop_value in properties.items():
 48            try:
 49                prop = WallSwitchProps(prop_key)
 50                device_props.append((prop, prop_value))
 51            except ValueError as e:
 52                _LOGGER.debug(f"{e} with value {prop_value}")
 53
 54        for prop, value in device_props:
 55            if prop == WallSwitchProps.IOT_STATE:
 56                switch.available = value == "connected"
 57            elif prop == WallSwitchProps.SWITCH_POWER:
 58                switch.switch_power = value
 59            elif prop == WallSwitchProps.SWITCH_IOT:
 60                switch.switch_iot = value
 61            elif prop == WallSwitchProps.SINGLE_PRESS_TYPE:
 62                switch.single_press_type = SinglePressType(value)
 63
 64        return switch
 65
 66    async def get_switches(self) -> List[WallSwitch]:
 67        if self._devices is None:
 68            self._devices = await self.get_object_list()
 69
 70        switches = [device for device in self._devices
 71                    if device.type is DeviceTypes.COMMON 
 72                    and device.product_model == "LD_SS1"]
 73
 74        return [WallSwitch(switch.raw_dict) for switch in switches]
 75
 76    async def turn_on(self, switch: WallSwitch):
 77        if switch.single_press_type == SinglePressType.IOT:
 78            await self.iot_on(switch)
 79        else:
 80            await self.power_on(switch)
 81
 82    async def turn_off(self, switch: WallSwitch):
 83        if switch.single_press_type == SinglePressType.IOT:
 84            await self.iot_off(switch)
 85        else:
 86            await self.power_off(switch)
 87
 88    async def power_on(self, switch: WallSwitch):
 89        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, True)
 90
 91    async def power_off(self, switch: WallSwitch):
 92        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, False)
 93
 94    async def iot_on(self, switch: WallSwitch):
 95        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, True)
 96
 97    async def iot_off(self, switch: WallSwitch):
 98        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, False)
 99
100    async def set_single_press_type(self, switch: WallSwitch, single_press_type: SinglePressType):
101        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SINGLE_PRESS_TYPE, single_press_type.value)
102
103    async def _wall_switch_get_iot_prop(self, device: Device) -> Dict[Any, Any]:
104        url = "https://wyze-sirius-service.wyzecam.com//plugin/sirius/get_iot_prop"
105        keys = "iot_state,switch-power,switch-iot,single_press_type"
106        return await self._get_iot_prop(url, device, keys)
107
108    async def _wall_switch_set_iot_prop(self, device: Device, prop: WallSwitchProps, value: Any) -> None:
109        url = "https://wyze-sirius-service.wyzecam.com//plugin/sirius/set_iot_prop_by_topic"
110        return await self._set_iot_prop(url, device, prop.value, value)
class SinglePressType(enum.Enum):
17class SinglePressType(Enum):
18    CLASSIC = 1
19    IOT = 2
CLASSIC = <SinglePressType.CLASSIC: 1>
IOT = <SinglePressType.IOT: 2>
class WallSwitch(wyzeapy.types.Device):
22class WallSwitch(Device):
23    def __init__(self, dictionary: Dict[Any, Any]):
24        super().__init__(dictionary)
25
26        self.switch_power: bool = False
27        self.switch_iot: bool = False
28        self.single_press_type: SinglePressType = SinglePressType.CLASSIC
29
30    @property
31    def on(self):
32        if self.single_press_type == SinglePressType.IOT:
33            return self.switch_iot
34        return self.switch_power
35
36    @on.setter
37    def on(self, state: bool):
38        if self.single_press_type == SinglePressType.IOT:
39            self.switch_iot = state
40        self.switch_power = state
WallSwitch(dictionary: Dict[Any, Any])
23    def __init__(self, dictionary: Dict[Any, Any]):
24        super().__init__(dictionary)
25
26        self.switch_power: bool = False
27        self.switch_iot: bool = False
28        self.single_press_type: SinglePressType = SinglePressType.CLASSIC
switch_power: bool
switch_iot: bool
single_press_type: SinglePressType
on
30    @property
31    def on(self):
32        if self.single_press_type == SinglePressType.IOT:
33            return self.switch_iot
34        return self.switch_power
class WallSwitchService(wyzeapy.services.base_service.BaseService):
 43class WallSwitchService(BaseService):
 44    async def update(self, switch: WallSwitch) -> WallSwitch:
 45        properties = (await self._wall_switch_get_iot_prop(switch))['data']['props']
 46
 47        device_props = []
 48        for prop_key, prop_value in properties.items():
 49            try:
 50                prop = WallSwitchProps(prop_key)
 51                device_props.append((prop, prop_value))
 52            except ValueError as e:
 53                _LOGGER.debug(f"{e} with value {prop_value}")
 54
 55        for prop, value in device_props:
 56            if prop == WallSwitchProps.IOT_STATE:
 57                switch.available = value == "connected"
 58            elif prop == WallSwitchProps.SWITCH_POWER:
 59                switch.switch_power = value
 60            elif prop == WallSwitchProps.SWITCH_IOT:
 61                switch.switch_iot = value
 62            elif prop == WallSwitchProps.SINGLE_PRESS_TYPE:
 63                switch.single_press_type = SinglePressType(value)
 64
 65        return switch
 66
 67    async def get_switches(self) -> List[WallSwitch]:
 68        if self._devices is None:
 69            self._devices = await self.get_object_list()
 70
 71        switches = [device for device in self._devices
 72                    if device.type is DeviceTypes.COMMON 
 73                    and device.product_model == "LD_SS1"]
 74
 75        return [WallSwitch(switch.raw_dict) for switch in switches]
 76
 77    async def turn_on(self, switch: WallSwitch):
 78        if switch.single_press_type == SinglePressType.IOT:
 79            await self.iot_on(switch)
 80        else:
 81            await self.power_on(switch)
 82
 83    async def turn_off(self, switch: WallSwitch):
 84        if switch.single_press_type == SinglePressType.IOT:
 85            await self.iot_off(switch)
 86        else:
 87            await self.power_off(switch)
 88
 89    async def power_on(self, switch: WallSwitch):
 90        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, True)
 91
 92    async def power_off(self, switch: WallSwitch):
 93        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, False)
 94
 95    async def iot_on(self, switch: WallSwitch):
 96        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, True)
 97
 98    async def iot_off(self, switch: WallSwitch):
 99        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, False)
100
101    async def set_single_press_type(self, switch: WallSwitch, single_press_type: SinglePressType):
102        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SINGLE_PRESS_TYPE, single_press_type.value)
103
104    async def _wall_switch_get_iot_prop(self, device: Device) -> Dict[Any, Any]:
105        url = "https://wyze-sirius-service.wyzecam.com//plugin/sirius/get_iot_prop"
106        keys = "iot_state,switch-power,switch-iot,single_press_type"
107        return await self._get_iot_prop(url, device, keys)
108
109    async def _wall_switch_set_iot_prop(self, device: Device, prop: WallSwitchProps, value: Any) -> None:
110        url = "https://wyze-sirius-service.wyzecam.com//plugin/sirius/set_iot_prop_by_topic"
111        return await self._set_iot_prop(url, device, prop.value, value)

Base service class for interacting with Wyze devices.

async def update( self, switch: WallSwitch) -> WallSwitch:
44    async def update(self, switch: WallSwitch) -> WallSwitch:
45        properties = (await self._wall_switch_get_iot_prop(switch))['data']['props']
46
47        device_props = []
48        for prop_key, prop_value in properties.items():
49            try:
50                prop = WallSwitchProps(prop_key)
51                device_props.append((prop, prop_value))
52            except ValueError as e:
53                _LOGGER.debug(f"{e} with value {prop_value}")
54
55        for prop, value in device_props:
56            if prop == WallSwitchProps.IOT_STATE:
57                switch.available = value == "connected"
58            elif prop == WallSwitchProps.SWITCH_POWER:
59                switch.switch_power = value
60            elif prop == WallSwitchProps.SWITCH_IOT:
61                switch.switch_iot = value
62            elif prop == WallSwitchProps.SINGLE_PRESS_TYPE:
63                switch.single_press_type = SinglePressType(value)
64
65        return switch
async def get_switches(self) -> List[WallSwitch]:
67    async def get_switches(self) -> List[WallSwitch]:
68        if self._devices is None:
69            self._devices = await self.get_object_list()
70
71        switches = [device for device in self._devices
72                    if device.type is DeviceTypes.COMMON 
73                    and device.product_model == "LD_SS1"]
74
75        return [WallSwitch(switch.raw_dict) for switch in switches]
async def turn_on(self, switch: WallSwitch):
77    async def turn_on(self, switch: WallSwitch):
78        if switch.single_press_type == SinglePressType.IOT:
79            await self.iot_on(switch)
80        else:
81            await self.power_on(switch)
async def turn_off(self, switch: WallSwitch):
83    async def turn_off(self, switch: WallSwitch):
84        if switch.single_press_type == SinglePressType.IOT:
85            await self.iot_off(switch)
86        else:
87            await self.power_off(switch)
async def power_on(self, switch: WallSwitch):
89    async def power_on(self, switch: WallSwitch):
90        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, True)
async def power_off(self, switch: WallSwitch):
92    async def power_off(self, switch: WallSwitch):
93        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_POWER, False)
async def iot_on(self, switch: WallSwitch):
95    async def iot_on(self, switch: WallSwitch):
96        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, True)
async def iot_off(self, switch: WallSwitch):
98    async def iot_off(self, switch: WallSwitch):
99        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SWITCH_IOT, False)
async def set_single_press_type( self, switch: WallSwitch, single_press_type: SinglePressType):
101    async def set_single_press_type(self, switch: WallSwitch, single_press_type: SinglePressType):
102        await self._wall_switch_set_iot_prop(switch, WallSwitchProps.SINGLE_PRESS_TYPE, single_press_type.value)