wyzeapy.services.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
 6from typing import List, Dict, Any
 7
 8from .base_service import BaseService
 9from ..types import Device, DeviceTypes, PropertyIDs
10from datetime import timedelta, datetime
11
12
13class Switch(Device):
14    def __init__(self, dictionary: Dict[Any, Any]):
15        super().__init__(dictionary)
16        self.on: bool = False
17
18
19class SwitchService(BaseService):
20    async def update(self, switch: Switch):
21        # Get updated device_params
22        async with BaseService._update_lock:
23            switch.device_params = await self.get_updated_params(switch.mac)
24
25        device_info = await self._get_property_list(switch)
26
27        for property_id, value in device_info:
28            if property_id == PropertyIDs.ON:
29                switch.on = value == "1"
30            elif property_id == PropertyIDs.AVAILABLE:
31                switch.available = value == "1"
32
33        return switch
34
35    async def get_switches(self) -> List[Switch]:
36        if self._devices is None:
37            self._devices = await self.get_object_list()
38
39        devices = [device for device in self._devices if device.type is DeviceTypes.PLUG or
40                   device.type is DeviceTypes.OUTDOOR_PLUG]
41        return [Switch(switch.raw_dict) for switch in devices]
42
43    async def turn_on(self, switch: Switch):
44        await self._set_property(switch, PropertyIDs.ON.value, "1")
45
46    async def turn_off(self, switch: Switch):
47        await self._set_property(switch, PropertyIDs.ON.value, "0")
48
49
50class SwitchUsageService(SwitchService):
51    """Class to retrieve the last 25 hours of usage data."""
52
53    async def update(self, device: Device):
54        start_time = int(
55            datetime.timestamp((datetime.now() - timedelta(hours=25))) * 1000
56        )
57        end_time = int(datetime.timestamp(datetime.now()) * 1000)
58
59        device.usage_history = await self._get_plug_history(
60            device, start_time, end_time
61        )
62
63        return device
class Switch(wyzeapy.types.Device):
14class Switch(Device):
15    def __init__(self, dictionary: Dict[Any, Any]):
16        super().__init__(dictionary)
17        self.on: bool = False
Switch(dictionary: Dict[Any, Any])
15    def __init__(self, dictionary: Dict[Any, Any]):
16        super().__init__(dictionary)
17        self.on: bool = False
on: bool
class SwitchService(wyzeapy.services.base_service.BaseService):
20class SwitchService(BaseService):
21    async def update(self, switch: Switch):
22        # Get updated device_params
23        async with BaseService._update_lock:
24            switch.device_params = await self.get_updated_params(switch.mac)
25
26        device_info = await self._get_property_list(switch)
27
28        for property_id, value in device_info:
29            if property_id == PropertyIDs.ON:
30                switch.on = value == "1"
31            elif property_id == PropertyIDs.AVAILABLE:
32                switch.available = value == "1"
33
34        return switch
35
36    async def get_switches(self) -> List[Switch]:
37        if self._devices is None:
38            self._devices = await self.get_object_list()
39
40        devices = [device for device in self._devices if device.type is DeviceTypes.PLUG or
41                   device.type is DeviceTypes.OUTDOOR_PLUG]
42        return [Switch(switch.raw_dict) for switch in devices]
43
44    async def turn_on(self, switch: Switch):
45        await self._set_property(switch, PropertyIDs.ON.value, "1")
46
47    async def turn_off(self, switch: Switch):
48        await self._set_property(switch, PropertyIDs.ON.value, "0")

Base service class for interacting with Wyze devices.

async def update(self, switch: Switch):
21    async def update(self, switch: Switch):
22        # Get updated device_params
23        async with BaseService._update_lock:
24            switch.device_params = await self.get_updated_params(switch.mac)
25
26        device_info = await self._get_property_list(switch)
27
28        for property_id, value in device_info:
29            if property_id == PropertyIDs.ON:
30                switch.on = value == "1"
31            elif property_id == PropertyIDs.AVAILABLE:
32                switch.available = value == "1"
33
34        return switch
async def get_switches(self) -> List[Switch]:
36    async def get_switches(self) -> List[Switch]:
37        if self._devices is None:
38            self._devices = await self.get_object_list()
39
40        devices = [device for device in self._devices if device.type is DeviceTypes.PLUG or
41                   device.type is DeviceTypes.OUTDOOR_PLUG]
42        return [Switch(switch.raw_dict) for switch in devices]
async def turn_on(self, switch: Switch):
44    async def turn_on(self, switch: Switch):
45        await self._set_property(switch, PropertyIDs.ON.value, "1")
async def turn_off(self, switch: Switch):
47    async def turn_off(self, switch: Switch):
48        await self._set_property(switch, PropertyIDs.ON.value, "0")
class SwitchUsageService(SwitchService):
51class SwitchUsageService(SwitchService):
52    """Class to retrieve the last 25 hours of usage data."""
53
54    async def update(self, device: Device):
55        start_time = int(
56            datetime.timestamp((datetime.now() - timedelta(hours=25))) * 1000
57        )
58        end_time = int(datetime.timestamp(datetime.now()) * 1000)
59
60        device.usage_history = await self._get_plug_history(
61            device, start_time, end_time
62        )
63
64        return device

Class to retrieve the last 25 hours of usage data.

async def update(self, device: wyzeapy.types.Device):
54    async def update(self, device: Device):
55        start_time = int(
56            datetime.timestamp((datetime.now() - timedelta(hours=25))) * 1000
57        )
58        end_time = int(datetime.timestamp(datetime.now()) * 1000)
59
60        device.usage_history = await self._get_plug_history(
61            device, start_time, end_time
62        )
63
64        return device