Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose sensors, switches, and settings for zhimi.airhumidifier #1508

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions miio/integrations/humidifier/zhimi/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from miio import Device, DeviceError, DeviceException, DeviceInfo, DeviceStatus
from miio.click_common import EnumType, command, format_output
from miio.devicestatus import sensor, setting, switch

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -99,6 +100,7 @@ def mode(self) -> OperationMode:
return OperationMode(self.data["mode"])

@property
@sensor("Temperature", unit="°C", device_class="temperature")
rytilahti marked this conversation as resolved.
Show resolved Hide resolved
def temperature(self) -> Optional[float]:
"""Current temperature, if available."""
if "temp_dec" in self.data and self.data["temp_dec"] is not None:
Expand All @@ -108,23 +110,45 @@ def temperature(self) -> Optional[float]:
return None

@property
@sensor("Humidity", unit="%", device_class="humidity")
def humidity(self) -> int:
"""Current humidity."""
return self.data["humidity"]

@property
@switch(
name="Buzzer",
icon="mdi:volume-high",
setter_name="set_buzzer",
device_class="switch",
entity_category="config",
)
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"] == "on"

@property
@setting(
name="Led Brightness",
icon="mdi:brightness-6",
setter_name="set_led_brightness",
choices=LedBrightness,
entity_category="config",
)
def led_brightness(self) -> Optional[LedBrightness]:
"""LED brightness if available."""
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])
return None

@property
@switch(
name="Child Lock",
icon="mdi:lock",
setter_name="set_child_lock",
device_class="switch",
entity_category="config",
)
def child_lock(self) -> bool:
"""Return True if child lock is on."""
return self.data["child_lock"] == "on"
Expand Down Expand Up @@ -183,8 +207,15 @@ def firmware_version_minor(self) -> int:
return 0

@property
@sensor(
"Motor Speed",
unit="rpm",
device_class="measurement",
icon="mdi:fast-forward",
entity_category="diagnostic",
)
def motor_speed(self) -> Optional[int]:
"""Current fan speed."""
"""Current motor speed."""
if "speed" in self.data and self.data["speed"] is not None:
return self.data["speed"]
return None
Expand All @@ -193,13 +224,20 @@ def motor_speed(self) -> Optional[int]:
def depth(self) -> Optional[int]:
"""Return raw value of depth."""
_LOGGER.warning(
"The 'depth' property is deprecated and will be removed in the future. Use 'water_level' and 'water_tank_detached' properties instead."
"The 'depth' property is deprecated and will be removed in the future. Use 'water_level' and 'water_tank_attached' properties instead."
)
if "depth" in self.data:
return self.data["depth"]
return None

@property
@sensor(
"Water Level",
unit="%",
device_class="measurement",
icon="mdi:water-check",
entity_category="diagnostic",
)
def water_level(self) -> Optional[int]:
"""Return current water level in percent.

Expand All @@ -214,17 +252,44 @@ def water_level(self) -> Optional[int]:

return int(min(depth / 1.2, 100))

@property
@sensor(
"Water Tank Attached",
device_class="connectivity",
icon="mdi:car-coolant-level",
entity_category="diagnostic",
)
def water_tank_attached(self) -> Optional[bool]:
"""True if the water tank is attached.

If water tank is detached, depth is 127.
"""
if self.data.get("depth") is not None:
return self.data["depth"] != 127
return None

@property
def water_tank_detached(self) -> Optional[bool]:
"""True if the water tank is detached.

If water tank is detached, depth is 127.
"""

_LOGGER.warning(
"The 'water_tank_detached' property is deprecated and will be removed in the future. Use 'water_tank_attached' properties instead."
)
if self.data.get("depth") is not None:
return self.data["depth"] == 127
return None

@property
@switch(
name="Dry Mode",
icon="mdi:hair-dryer",
setter_name="set_dry",
device_class="switch",
entity_category="config",
)
def dry(self) -> Optional[bool]:
"""Dry mode: The amount of water is not enough to continue to work for about 8
hours.
Expand All @@ -236,6 +301,13 @@ def dry(self) -> Optional[bool]:
return None

@property
@sensor(
"Use Time",
unit="s",
device_class="total_increasing",
icon="mdi:progress-clock",
entity_category="diagnostic",
)
def use_time(self) -> Optional[int]:
"""How long the device has been active in seconds."""
return self.data["use_time"]
Expand Down Expand Up @@ -273,7 +345,7 @@ class AirHumidifier(Device):
"Speed: {result.motor_speed}\n"
"Depth: {result.depth}\n"
"Water Level: {result.water_level} %\n"
"Water tank detached: {result.water_tank_detached}\n"
"Water tank attached: {result.water_tank_attached}\n"
"Dry: {result.dry}\n"
"Use time: {result.use_time}\n"
"Hardware version: {result.hardware_version}\n"
Expand Down