|
| 1 | +from abc import abstractmethod |
| 2 | +import logging |
| 3 | + |
| 4 | +from .things import Thing |
| 5 | +from .utils import WrapperCallback |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class Fan(Thing): |
| 11 | + """Basic class for a Fan entity. |
| 12 | + |
| 13 | + Fan entities can have quite a lot of behavior (oscillation, direction, |
| 14 | + presets...). For now, fans only consider the speed, set through a single |
| 15 | + `command_topic`. |
| 16 | + """ |
| 17 | + optimistic: bool |
| 18 | + speed_range_max: int |
| 19 | + speed_range_min: int |
| 20 | + |
| 21 | + config_fields = ["optimistic", "speed_range_max", "speed_range_min"] |
| 22 | + |
| 23 | + _state: bool = False |
| 24 | + _speed: int # Used only for fans with speed |
| 25 | + |
| 26 | + @property |
| 27 | + def component(self): |
| 28 | + return "fan" |
| 29 | + |
| 30 | + @abstractmethod |
| 31 | + def callback(self, state: bool): |
| 32 | + pass |
| 33 | + |
| 34 | + def raw_callback(self, topic, payload): |
| 35 | + print("Here: %s %s" % (topic, payload)) |
| 36 | + if payload == b'ON': |
| 37 | + self._state = True |
| 38 | + return self.callback(True) |
| 39 | + elif payload == b'OFF': |
| 40 | + self._state = False |
| 41 | + return self.callback(False) |
| 42 | + else: |
| 43 | + logger.warning("Ignoring unrecognized command: `%s`", payload) |
| 44 | + |
| 45 | + def get_config(self): |
| 46 | + config = super().get_config() |
| 47 | + config["command_topic"] = f'~/{ self.short_id }/set' |
| 48 | + return config |
| 49 | + |
| 50 | + def set_callbacks(self): |
| 51 | + self.mqtt_manager.client.message_callback_add( |
| 52 | + f'{ self.mqtt_manager.base_topic }/{ self.short_id }/set', |
| 53 | + WrapperCallback(self.raw_callback) |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class BinaryOptimisticFan(Fan): |
| 58 | + """A fan that tracks its own on/off state.""" |
| 59 | + @property |
| 60 | + def state(self): |
| 61 | + return self._state |
| 62 | + |
| 63 | + @state.setter |
| 64 | + def state(self, value: bool): |
| 65 | + self._state = value |
| 66 | + self.publish_state(self._state) |
| 67 | + |
| 68 | + def callback(self, state: bool): |
| 69 | + self.state = state |
| 70 | + |
| 71 | + def get_config(self): |
| 72 | + config = super().get_config() |
| 73 | + config["state_topic"] = f'~/{ self.short_id }/main' |
| 74 | + return config |
| 75 | + |
| 76 | + |
| 77 | +class PercentageOptimisticFan(BinaryOptimisticFan): |
| 78 | + """A Fan that has regulable speed.""" |
| 79 | + |
| 80 | + speed_range_min = 1 |
| 81 | + speed_range_max = 100 |
| 82 | + _speed = 1 |
| 83 | + |
| 84 | + @property |
| 85 | + def speed(self): |
| 86 | + return self._speed |
| 87 | + |
| 88 | + @speed.setter |
| 89 | + def speed(self, value: int): |
| 90 | + if value == 0: |
| 91 | + self.state = False |
| 92 | + # don't touch the _speed, it works as the "last known speed" |
| 93 | + else: |
| 94 | + self.state = True |
| 95 | + |
| 96 | + self._speed = value |
| 97 | + self.publish_mqtt_message(bytes(str(value), "utf-8"), "speed/state") |
| 98 | + self.publish_state(self._state) |
| 99 | + |
| 100 | + def speed_callback(self, speed: int): |
| 101 | + self.speed = speed |
| 102 | + |
| 103 | + def raw_speed_callback(self, topic: str, raw_speed: bytes): |
| 104 | + print("Hey!") |
| 105 | + self.speed_callback(int(raw_speed.decode("utf-8"))) |
| 106 | + |
| 107 | + def get_config(self): |
| 108 | + config = super().get_config() |
| 109 | + config["percentage_state_topic"] = f'~/{ self.short_id }/speed/state' |
| 110 | + config["percentage_command_topic"] = f'~/{ self.short_id }/speed/set' |
| 111 | + return config |
| 112 | + |
| 113 | + def set_callbacks(self): |
| 114 | + super().set_callbacks() |
| 115 | + self.mqtt_manager.client.message_callback_add( |
| 116 | + f'{ self.mqtt_manager.base_topic }/{ self.short_id }/speed/set', |
| 117 | + WrapperCallback(self.raw_speed_callback) |
| 118 | + ) |
0 commit comments