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
|
import enum
import logging
from typing import Any, Dict, List
import click
from .click_common import EnumType, command, format_output
from .device import Device, DeviceStatus
_LOGGER = logging.getLogger(__name__)
MODEL_TOILETLID_V1 = "tinymu.toiletlid.v1"
AVAILABLE_PROPERTIES_COMMON = ["work_state", "filter_use_flux", "filter_use_time"]
AVAILABLE_PROPERTIES = {MODEL_TOILETLID_V1: AVAILABLE_PROPERTIES_COMMON}
class AmbientLightColor(enum.Enum):
White = "0"
Yellow = "1"
Powder = "2"
Green = "3"
Purple = "4"
Blue = "5"
Orange = "6"
Red = "7"
class ToiletlidOperatingMode(enum.Enum):
Vacant = 0
Occupied = 1
RearCleanse = 2
FrontCleanse = 3
NozzleClean = 6
class ToiletlidStatus(DeviceStatus):
def __init__(self, data: Dict[str, Any]) -> None:
# {"work_state": 1,"filter_use_flux": 100,"filter_use_time": 180, "ambient_light": "Red"}
self.data = data
@property
def work_state(self) -> int:
"""Device state code."""
return self.data["work_state"]
@property
def work_mode(self) -> ToiletlidOperatingMode:
"""Device working mode."""
return ToiletlidOperatingMode((self.work_state - 1) // 16)
@property
def is_on(self) -> bool:
return self.work_state != 1
@property
def filter_use_percentage(self) -> str:
"""Filter percentage of remaining life."""
return "{}%".format(self.data["filter_use_flux"])
@property
def filter_remaining_time(self) -> int:
"""Filter remaining life days."""
return self.data["filter_use_time"]
@property
def ambient_light(self) -> str:
"""Ambient light color."""
return self.data["ambient_light"]
class Toiletlid(Device):
"""Support for tinymu.toiletlid.v1."""
_supported_models = list(AVAILABLE_PROPERTIES.keys())
@command(
default_output=format_output(
"",
"Work: {result.is_on}\n"
"State: {result.work_state}\n"
"Work Mode: {result.work_mode}\n"
"Ambient Light: {result.ambient_light}\n"
"Filter remaining: {result.filter_use_percentage}\n"
"Filter remaining time: {result.filter_remaining_time}\n",
)
)
def status(self) -> ToiletlidStatus:
"""Retrieve properties."""
properties = AVAILABLE_PROPERTIES.get(
self.model, AVAILABLE_PROPERTIES[MODEL_TOILETLID_V1]
)
values = self.get_properties(properties)
color = self.get_ambient_light()
return ToiletlidStatus(dict(zip(properties, values), ambient_light=color))
@command(default_output=format_output("Nozzle clean"))
def nozzle_clean(self):
"""Nozzle clean."""
return self.send("nozzle_clean", ["on"])
@command(
click.argument("color", type=EnumType(AmbientLightColor)),
click.argument("xiaomi_id", type=str, default=""),
default_output=format_output(
"Set the ambient light to {color} color the next time you start it."
),
)
def set_ambient_light(self, color: AmbientLightColor, xiaomi_id: str = ""):
"""Set Ambient light color."""
return self.send("set_aled_v_of_uid", [xiaomi_id, color.value])
@command(
click.argument("xiaomi_id", type=str, default=""),
default_output=format_output("Get the Ambient light color."),
)
def get_ambient_light(self, xiaomi_id: str = "") -> str:
"""Get Ambient light color."""
color = self.send("get_aled_v_of_uid", [xiaomi_id])
try:
return AmbientLightColor(color[0]).name
except ValueError:
_LOGGER.warning(
"Get ambient light response error, return unknown value: %s.", color[0]
)
return "Unknown"
@command(default_output=format_output("Get user list."))
def get_all_user_info(self) -> List[Dict]:
"""Get All bind user."""
users = self.send("get_all_user_info")
return users
@command(
click.argument("xiaomi_id", type=str),
click.argument("band_mac", type=str),
click.argument("alias", type=str),
default_output=format_output("Bind xiaomi band to xiaomi id."),
)
def bind_xiaomi_band(self, xiaomi_id: str, band_mac: str, alias: str):
"""Bind xiaomi band to xiaomi id."""
return self.send("uid_mac_op", [xiaomi_id, band_mac, alias, "bind"])
@command(
click.argument("xiaomi_id", type=str),
click.argument("band_mac", type=str),
default_output=format_output("Unbind xiaomi band to xiaomi id."),
)
def unbind_xiaomi_band(self, xiaomi_id: str, band_mac: str):
"""Unbind xiaomi band to xiaomi id."""
return self.send("uid_mac_op", [xiaomi_id, band_mac, "", "unbind"])
|