Skip to content

Commit

Permalink
feat(c3): add disinfect and fix tbh set error
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwentao committed Dec 13, 2024
1 parent e1a3855 commit dc26e85
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
6 changes: 3 additions & 3 deletions midealocal/devices/c3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MessageC3Response,
MessageQuery,
MessageQueryBasic,
MessageQueryDisinfect,
MessageQuerySilence,
MessageSet,
MessageSetECO,
Expand Down Expand Up @@ -117,6 +118,7 @@ def build_query(self) -> list[MessageQuery]:
"""Midea C3 device build query."""
return [
MessageQueryBasic(self._message_protocol_version),
MessageQueryDisinfect(self._message_protocol_version),
MessageQuerySilence(self._message_protocol_version),
]

Expand Down Expand Up @@ -221,7 +223,6 @@ def make_message_set(self) -> MessageSet:
message.room_target_temp = self._attributes[DeviceAttributes.room_target_temp]
message.zone1_curve = self._attributes[DeviceAttributes.zone1_curve]
message.zone2_curve = self._attributes[DeviceAttributes.zone2_curve]
message.disinfect = self._attributes[DeviceAttributes.disinfect]
message.tbh = self._attributes[DeviceAttributes.tbh]
message.fast_dhw = self._attributes[DeviceAttributes.fast_dhw]
return message
Expand All @@ -235,10 +236,9 @@ def set_attribute(self, attr: str, value: bool | int | str) -> None:
DeviceAttributes.dhw_power,
DeviceAttributes.zone1_curve,
DeviceAttributes.zone2_curve,
DeviceAttributes.disinfect,
DeviceAttributes.tbh,
DeviceAttributes.fast_dhw,
DeviceAttributes.dhw_target_temp,
DeviceAttributes.tbh,
]:
message = self.make_message_set()
setattr(message, str(attr), value)
Expand Down
76 changes: 60 additions & 16 deletions midealocal/devices/c3/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def __init__(self, protocol_version: int) -> None:
super().__init__(protocol_version, ListTypes.X05)


class MessageQueryDisinfect(MessageQuery):
"""C3 Message query Disinfect."""

def __init__(self, protocol_version: int) -> None:
"""Initialize C3 message query silence."""
super().__init__(protocol_version, ListTypes.X09)


class MessageSet(MessageC3Base):
"""C3 message set."""

Expand All @@ -87,7 +95,6 @@ def __init__(self, protocol_version: int) -> None:
self.room_target_temp = 25.0
self.zone1_curve = False
self.zone2_curve = False
self.disinfect = False
self.fast_dhw = False
self.tbh = False

Expand All @@ -100,7 +107,7 @@ def _body(self) -> bytearray:
# Byte 7
zone1_curve = 0x01 if self.zone1_curve else 0x00
zone2_curve = 0x02 if self.zone2_curve else 0x00
disinfect = 0x04 if self.disinfect or self.tbh else 0x00
tbh = 0x04 if self.tbh else 0x00
fast_dhw = 0x08 if self.fast_dhw else 0x00
room_target_temp = int(self.room_target_temp * 2)
zone1_target_temp = int(self.zone_target_temp[0])
Expand All @@ -114,7 +121,7 @@ def _body(self) -> bytearray:
zone2_target_temp,
dhw_target_temp,
room_target_temp,
zone1_curve | zone2_curve | disinfect | fast_dhw,
zone1_curve | zone2_curve | tbh | fast_dhw,
],
)

Expand Down Expand Up @@ -174,25 +181,40 @@ class C3MessageBody(MessageBody):
def __init__(self, body: bytearray, data_offset: int = 0) -> None:
"""Initialize C3 message body."""
super().__init__(body)
# BodyBytes 1
self.zone1_power = body[data_offset + 0] & 0x01 > 0
self.zone2_power = body[data_offset + 0] & 0x02 > 0
self.dhw_power = body[data_offset + 0] & 0x04 > 0
self.zone1_curve = body[data_offset + 0] & 0x08 > 0
self.zone2_curve = body[data_offset + 0] & 0x10 > 0
self.disinfect = body[data_offset + 0] & 0x20 > 0
self.tbh = body[data_offset + 0] & 0x20 > 0
self.fast_dhw = body[data_offset + 0] & 0x40 > 0
self.remote_onoff = body[data_offset + 0] & 0x80 > 0
# BodyBytes 2
self.heat = body[data_offset + 1] & 0x01 > 0
self.cool = body[data_offset + 1] & 0x02 > 0
self.dhw = body[data_offset + 1] & 0x04 > 0
self.double_zone = body[data_offset + 1] & 0x08 > 0
self.zone_temp_type = [
body[data_offset + 1] & 0x10 > 0,
body[data_offset + 1] & 0x20 > 0,
]
self.room_thermal_support = body[data_offset + 1] & 0x40 > 0
self.room_thermal_state = body[data_offset + 1] & 0x80 > 0
# BodyBytes 3
self.time_set = body[data_offset + 2] & 0x01 > 0
self.silent_mode = body[data_offset + 2] & 0x02 > 0
self.holiday_on = body[data_offset + 2] & 0x04 > 0
self.eco_mode = body[data_offset + 2] & 0x08 > 0
self.zone_terminal_type = body[data_offset + 2]
# BodyBytes 4
self.mode = body[data_offset + 3]
self.mode_auto = body[data_offset + 4]
# zone1, zone2
self.zone_target_temp = [body[data_offset + 5], body[data_offset + 6]]
self.dhw_target_temp = body[data_offset + 7]
self.room_target_temp = body[data_offset + 8] / 2
# zone1, zone2
self.zone_heating_temp_max = [body[data_offset + 9], body[data_offset + 13]]
self.zone_heating_temp_min = [body[data_offset + 10], body[data_offset + 14]]
self.zone_cooling_temp_max = [body[data_offset + 11], body[data_offset + 15]]
Expand All @@ -203,27 +225,34 @@ def __init__(self, body: bytearray, data_offset: int = 0) -> None:
self.dhw_temp_min = body[data_offset + 20]
self.tank_actual_temperature = body[data_offset + 21]
self.error_code = body[data_offset + 22]
self.tbh_control = body[data_offset + 23] & 0x80 > 0


class C3Notify1MessageBody(MessageBody):
"""C3 notify1 message body."""
class C3EnergyMessageBody(MessageBody):
"""C3 Energy MSG_TYPE_UP_POWER4 message body."""

def __init__(self, body: bytearray, data_offset: int = 0) -> None:
"""Initialize C3 notify1 message body."""
super().__init__(body)
status_byte = body[data_offset]
self.status_tbh = (status_byte & 0x08) > 0
self.status_dhw = (status_byte & 0x04) > 0
self.status_ibh = (status_byte & 0x02) > 0
# bit0
self.status_heating = (status_byte & 0x01) > 0

# bit1
self.status_cool = (status_byte & 0x02) > 0
# bit2
self.status_dhw = (status_byte & 0x04) > 0
# bit3
self.status_tbh = (status_byte & 0x08) > 0
# bit4
self.status_ibh = (status_byte & 0x10) > 0
# total_energy_consumption
self.total_energy_consumption = (
(body[data_offset + 1] << 32)
+ (body[data_offset + 2] << 16)
+ (body[data_offset + 3] << 8)
+ (body[data_offset + 4])
)

# total_produced_energy
self.total_produced_energy = (
(body[data_offset + 5] << 32)
+ (body[data_offset + 6] << 16)
Expand All @@ -236,11 +265,11 @@ def __init__(self, body: bytearray, data_offset: int = 0) -> None:
)


class C3QuerySilenceMessageBody(MessageBody):
"""C3 Query silence message body."""
class C3SilenceMessageBody(MessageBody):
"""C3 Silence message body."""

def __init__(self, body: bytearray, data_offset: int = 0) -> None:
"""Initialize C3 notify1 message body."""
"""Initialize C3 query silence message body."""
super().__init__(body)
self.silent_mode = body[data_offset] & 0x1 > 0
self.silent_level = C3SilentLevel(
Expand All @@ -263,6 +292,19 @@ def __init__(self, body: bytearray, data_offset: int = 0) -> None:
# silence_timer2_endmin: Byte 9


class C3DisinfectMessageBody(MessageBody):
"""C3 Disinfect message body."""

def __init__(self, body: bytearray, data_offset: int = 0) -> None:
"""Initialize C3 Disinfect message body."""
super().__init__(body)
self.disinfect = body[data_offset] & 0x01 > 0
self.disinfect_run = body[data_offset] & 0x02 > 0
self.disinfect_set_weekday = body[data_offset + 1]
self.disinfect_start_hour = body[data_offset + 2]
self.disinfect_start_minutes = body[data_offset + 3]


class MessageC3Response(MessageResponse):
"""C3 message response."""

Expand All @@ -278,7 +320,9 @@ def __init__(self, message: bytes) -> None:
elif (
self.message_type == MessageType.notify1 and self.body_type == ListTypes.X04
):
self.set_body(C3Notify1MessageBody(super().body, data_offset=1))
self.set_body(C3EnergyMessageBody(super().body, data_offset=1))
elif self.message_type == MessageType.query and self.body_type == ListTypes.X05:
self.set_body(C3QuerySilenceMessageBody(super().body, data_offset=1))
self.set_body(C3SilenceMessageBody(super().body, data_offset=1))
elif self.message_type == MessageType.query and self.body_type == ListTypes.X09:
self.set_body(C3DisinfectMessageBody(super().body, data_offset=1))
self.set_attr()

0 comments on commit dc26e85

Please sign in to comment.