From 592848a7997e999c2bc9cd937808027b69b53c72 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Mon, 3 Jun 2024 19:06:26 +0000 Subject: [PATCH] chore: typing c3 --- midealocal/devices/c3/__init__.py | 19 +++++++++++-------- midealocal/devices/c3/message.py | 30 ++++++++++++++++-------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/midealocal/devices/c3/__init__.py b/midealocal/devices/c3/__init__.py index cba35626..f7bb9df6 100644 --- a/midealocal/devices/c3/__init__.py +++ b/midealocal/devices/c3/__init__.py @@ -1,5 +1,6 @@ import logging import sys +from typing import Any from .message import ( MessageC3Response, @@ -75,7 +76,7 @@ def __init__( model: str, subtype: int, customize: str, - ): + ) -> None: super().__init__( name=name, device_id=device_id, @@ -131,10 +132,10 @@ def __init__( }, ) - def build_query(self): + def build_query(self) -> list[MessageQuery]: return [MessageQuery(self._protocol_version)] - def process_message(self, msg): + def process_message(self, msg: bytes) -> dict[str, Any]: message = MessageC3Response(msg) _LOGGER.debug(f"[{self.device_id}] Received: {message}") new_status = {} @@ -219,7 +220,7 @@ def process_message(self, msg): return new_status - def make_message_set(self): + def make_message_set(self) -> MessageSet: message = MessageSet(self._protocol_version) message.zone1_power = self._attributes[DeviceAttributes.zone1_power] message.zone2_power = self._attributes[DeviceAttributes.zone2_power] @@ -235,8 +236,8 @@ def make_message_set(self): message.fast_dhw = self._attributes[DeviceAttributes.fast_dhw] return message - def set_attribute(self, attr, value): - message = None + def set_attribute(self, attr: str, value: Any) -> None: + message: MessageSet | MessageSetECO | MessageSetSilent | None = None if attr in [ DeviceAttributes.zone1_power, DeviceAttributes.zone2_power, @@ -259,7 +260,7 @@ def set_attribute(self, attr, value): if message is not None: self.build_send(message) - def set_mode(self, zone, mode): + def set_mode(self, zone: int, mode: int) -> None: message = self.make_message_set() if zone == 0: message.zone1_power = True @@ -268,7 +269,9 @@ def set_mode(self, zone, mode): message.mode = mode self.build_send(message) - def set_target_temperature(self, zone, target_temperature, mode): + def set_target_temperature( + self, zone: int, target_temperature: int, mode: int + ) -> None: message = self.make_message_set() if self._attributes[DeviceAttributes.zone_temp_type][zone]: message.zone_target_temp[zone] = target_temperature diff --git a/midealocal/devices/c3/message.py b/midealocal/devices/c3/message.py index f93b9e16..3e706ff6 100644 --- a/midealocal/devices/c3/message.py +++ b/midealocal/devices/c3/message.py @@ -7,7 +7,9 @@ class MessageC3Base(MessageRequest): - def __init__(self, protocol_version, message_type, body_type): + def __init__( + self, protocol_version: int, message_type: int, body_type: int + ) -> None: super().__init__( device_type=0xC3, protocol_version=protocol_version, @@ -16,12 +18,12 @@ def __init__(self, protocol_version, message_type, body_type): ) @property - def _body(self): + def _body(self) -> bytearray: raise NotImplementedError class MessageQuery(MessageC3Base): - def __init__(self, protocol_version): + def __init__(self, protocol_version: int) -> None: super().__init__( protocol_version=protocol_version, message_type=MessageType.query, @@ -29,12 +31,12 @@ def __init__(self, protocol_version): ) @property - def _body(self): + def _body(self) -> bytearray: return bytearray([]) class MessageSet(MessageC3Base): - def __init__(self, protocol_version): + def __init__(self, protocol_version: int) -> None: super().__init__( protocol_version=protocol_version, message_type=MessageType.set, @@ -54,7 +56,7 @@ def __init__(self, protocol_version): self.tbh = False @property - def _body(self): + def _body(self) -> bytearray: # Byte 1 zone1_power = 0x01 if self.zone1_power else 0x00 zone2_power = 0x02 if self.zone2_power else 0x00 @@ -82,7 +84,7 @@ def _body(self): class MessageSetSilent(MessageC3Base): - def __init__(self, protocol_version): + def __init__(self, protocol_version: int) -> None: super().__init__( protocol_version=protocol_version, message_type=MessageType.set, @@ -92,7 +94,7 @@ def __init__(self, protocol_version): self.super_silent = False @property - def _body(self): + def _body(self) -> bytearray: silent_mode = 0x01 if self.silent_mode else 0 super_silent = 0x02 if self.super_silent else 0 @@ -112,7 +114,7 @@ def _body(self): class MessageSetECO(MessageC3Base): - def __init__(self, protocol_version): + def __init__(self, protocol_version: int) -> None: super().__init__( protocol_version=protocol_version, message_type=MessageType.set, @@ -121,14 +123,14 @@ def __init__(self, protocol_version): self.eco_mode = False @property - def _body(self): + def _body(self) -> bytearray: eco_mode = 0x01 if self.eco_mode else 0 return bytearray([eco_mode, 0x00, 0x00, 0x00, 0x00, 0x00]) class C3MessageBody(MessageBody): - def __init__(self, body, data_offset=0): + def __init__(self, body: bytearray, data_offset: int = 0) -> None: super().__init__(body) self.zone1_power = body[data_offset + 0] & 0x01 > 0 self.zone2_power = body[data_offset + 0] & 0x02 > 0 @@ -162,7 +164,7 @@ def __init__(self, body, data_offset=0): class C3Notify1MessageBody(MessageBody): - def __init__(self, body, data_offset=0): + def __init__(self, body: bytearray, data_offset: int = 0) -> None: super().__init__(body) status_byte = body[data_offset] self.status_tbh = (status_byte & 0x08) > 0 @@ -190,8 +192,8 @@ def __init__(self, body, data_offset=0): class MessageC3Response(MessageResponse): - def __init__(self, message): - super().__init__(message) + def __init__(self, message: bytes) -> None: + super().__init__(bytearray(message)) if ( self.message_type in [MessageType.set, MessageType.notify1, MessageType.query]