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

feat(db): add new attributes for db device #329

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
200 changes: 197 additions & 3 deletions midealocal/devices/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from enum import StrEnum
from typing import Any
from typing import Any, ClassVar

from midealocal.const import DeviceType, ProtocolVersion
from midealocal.device import MideaDevice
Expand All @@ -18,6 +18,18 @@ class DeviceAttributes(StrEnum):

power = "power"
start = "start"
status = "status"
mode = "mode"
program = "program"
water_level = "water_level"
temperature = "temperature"
dehydration_speed = "dehydration_speed"
wash_time = "wash_time"
wash_time_value = "wash_time_value"
dehydration_time = "dehydration_time"
dehydration_time_value = "dehydration_time_value"
detergent = "detergent"
softener = "softener"
washing_data = "washing_data"
progress = "progress"
time_remaining = "time_remaining"
Expand All @@ -26,6 +38,134 @@ class DeviceAttributes(StrEnum):
class MideaDBDevice(MideaDevice):
"""Midea DB device."""

_status: ClassVar[dict[int, str]] = {
0: "idle",
1: "standby",
2: "start",
3: "pause",
4: "end",
5: "fault",
6: "delay",
}

_dehydration_speed: ClassVar[dict[int, str]] = {
0x00: "0",
0x01: "400",
0x02: "600",
0x03: "800",
0x04: "1000",
0x05: "1200",
0x06: "1400",
0x07: "1600",
0x08: "1300",
0xFF: "default",
}

_water_level: ClassVar[dict[int, str]] = {
0x01: "Low",
0x02: "Mid",
0x03: "High",
0x04: "4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "translated" to a string: VeryHigh ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the origina value from lua script.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please share the LUA script

0x05: "Auto",
}

_program: ClassVar[dict[int, str]] = {
0x00: "cotton",
0x01: "eco",
0x02: "fast_wash",
0x03: "mixed_wash",
0x04: "fiber",
0x05: "wool",
0x06: "enzyme",
0x07: "ssp",
0x08: "sport_clothes",
0x09: "single_dehytration",
0x0A: "rinsing_dehydration",
0x0B: "big",
0x0C: "baby_clothes",
0x0D: "outdoor",
0x0E: "air_wash",
0x0F: "down_jacket",
0x10: "color",
0x11: "intelligent",
0x12: "quick_wash",
0x13: "kids",
0x14: "water_cotton",
0x15: "single_drying",
0x16: "single_drying",
0x17: "fast_wash_30",
0x18: "fast_wash_60",
0x19: "water_intelligent",
0x1A: "water_steep",
0x1B: "water_fast_wash_30",
0x1C: "shirt",
0x1D: "steep",
0x1E: "new_water_cotton",
0x1F: "water_mixed_wash",
0x20: "water_fiber",
0x21: "water_kids",
0x22: "water_underwear",
0x23: "specialist",
0x24: "water_eco",
0x25: "wash_drying_60",
0x26: "self_wash_5",
0x27: "fast_wash_min",
0x28: "mixed_wash_min",
0x29: "dehydration_min",
0x2A: "self_wash_min",
0x2B: "baby_clothes_min",
0x2C: "prevent_allergy",
0x2D: "cold_wash",
0x2E: "soft_wash",
0x2F: "remove_mite_wash",
0x30: "water_intense_wash",
0x31: "fast_dry",
0x32: "water_outdoor",
0x33: "spring_autumn_wash",
0x34: "summer_wash",
0x35: "winter_wash",
0x36: "jean",
0x37: "new_clothes_wash",
0x38: "silk",
0x39: "insight_wash",
0x3A: "fitness_clothes",
0x3B: "mink",
0x3C: "fresh_air",
0x3D: "bucket_dry",
0x3E: "jacket",
0x3F: "bath_towel",
0x40: "night_fresh_wash",
0x50: "water_fiber",
0x51: "diy0",
0x52: "diy2",
0x60: "heart_wash",
0x61: "water_cold_wash",
0x62: "water_prevent_allergy",
0x63: "water_remove_mite_wash",
0x64: "water_ssp",
0x65: "silk_wash",
0x66: "standard",
0x67: "green_wool",
0x68: "cook_wash",
0x69: "fresh_remove_wrinkle",
0x6A: "steam_sterilize_wash",
0x6B: "aromatherapy",
0x70: "sterilize_wash",
0xFE: "love",
0xFF: "default",
}

_temperature: ClassVar[dict[int, str]] = {
0x01: "0",
0x02: "20",
0x03: "30",
0x04: "40",
0x05: "60",
0x06: "95",
0x07: "70",
wuwentao marked this conversation as resolved.
Show resolved Hide resolved
0xFF: "default",
}

def __init__(
self,
name: str,
Expand Down Expand Up @@ -54,9 +194,21 @@ def __init__(
attributes={
DeviceAttributes.power: False,
DeviceAttributes.start: False,
DeviceAttributes.status: None,
DeviceAttributes.mode: None,
DeviceAttributes.program: None,
DeviceAttributes.water_level: None,
DeviceAttributes.temperature: None,
DeviceAttributes.dehydration_speed: None,
DeviceAttributes.wash_time: None,
DeviceAttributes.dehydration_time: None,
DeviceAttributes.detergent: None,
DeviceAttributes.softener: None,
DeviceAttributes.washing_data: bytearray([]),
DeviceAttributes.progress: "Unknown",
DeviceAttributes.time_remaining: None,
DeviceAttributes.wash_time_value: None,
DeviceAttributes.dehydration_time_value: None,
},
)

Expand All @@ -82,10 +234,52 @@ def process_message(self, msg: bytes) -> dict[str, Any]:
]
for status in self._attributes:
if hasattr(message, str(status)):
value = getattr(message, str(status))
# parse progress
if status == DeviceAttributes.progress:
self._attributes[status] = progress[getattr(message, str(status))]
self._attributes[status] = progress[value]
# parse status
elif status == DeviceAttributes.status:
if value in MideaDBDevice._status:
self._attributes[DeviceAttributes.status] = (
MideaDBDevice._status.get(value)
)
else:
self._attributes[DeviceAttributes.status] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if value in MideaDBDevice._status:
self._attributes[DeviceAttributes.status] = (
MideaDBDevice._status.get(value)
)
else:
self._attributes[DeviceAttributes.status] = None
self._attributes[DeviceAttributes.status] = (
MideaDBDevice._status.get(value)
)

The purpose of get() is exactly to avoid the if

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value may not list in _status, just return None.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a very simple logic, if value not in list, we can't return error, just set the status to None.
so why you thinks the value always in _status ? we just list what we want to display, and it's not a full list with different sub modle for this device.
if there is any issue ,please add your comments, thanks

# parse dehydration_speed
elif status == DeviceAttributes.dehydration_speed:
if value in MideaDBDevice._dehydration_speed:
self._attributes[DeviceAttributes.dehydration_speed] = (
MideaDBDevice._dehydration_speed.get(value)
)
else:
self._attributes[DeviceAttributes.dehydration_speed] = None
wuwentao marked this conversation as resolved.
Show resolved Hide resolved
# parse water_level
elif status == DeviceAttributes.water_level:
if value in MideaDBDevice._water_level:
self._attributes[DeviceAttributes.water_level] = (
MideaDBDevice._water_level.get(value)
)
else:
self._attributes[DeviceAttributes.water_level] = None
wuwentao marked this conversation as resolved.
Show resolved Hide resolved
# parse program
elif status == DeviceAttributes.program:
if value in MideaDBDevice._program:
self._attributes[DeviceAttributes.program] = (
MideaDBDevice._program.get(value)
)
else:
self._attributes[DeviceAttributes.program] = None
wuwentao marked this conversation as resolved.
Show resolved Hide resolved
# parse temperature
elif status == DeviceAttributes.temperature:
if value in MideaDBDevice._temperature:
self._attributes[DeviceAttributes.temperature] = (
MideaDBDevice._temperature.get(value)
)
else:
self._attributes[DeviceAttributes.temperature] = None
wuwentao marked this conversation as resolved.
Show resolved Hide resolved
else:
self._attributes[status] = getattr(message, str(status))
self._attributes[status] = value
new_status[str(status)] = self._attributes[status]
return new_status

Expand Down
12 changes: 12 additions & 0 deletions midealocal/devices/db/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ def __init__(self, body: bytearray) -> None:
self.power = body[1] > 0
self.start = body[2] in [2, 6]
self.washing_data = body[3:16]
self.status = body[2]
self.mode = body[3]
self.program = body[4]
self.water_level = body[5]
self.temperature = body[7]
self.dehydration_speed = body[8]
self.wash_time = body[9]
self.dehydration_time = body[10]
self.detergent = body[11]
self.softener = body[12]
self.wash_time_value = body[27]
self.dehydration_time_value = body[28]
self.progress = 0
self.time_remaining: float | None = None
for i in range(7):
Expand Down
Loading