diff options
Diffstat (limited to 'src/pump_bot.py')
-rwxr-xr-x | src/pump_bot.py | 72 |
1 files changed, 62 insertions, 10 deletions
diff --git a/src/pump_bot.py b/src/pump_bot.py index d3aa6b0..0e2b71d 100755 --- a/src/pump_bot.py +++ b/src/pump_bot.py @@ -2,18 +2,33 @@ from enum import Enum from typing import Optional from telegram import ReplyKeyboardMarkup, User +from time import time +from datetime import datetime from home.config import config, is_development_mode from home.telegram import bot from home.telegram._botutil import user_any_name from home.relay.sunxi_h3_client import RelayClient from home.api.types import BotType -from home.mqtt import MqttNode, MqttModule, add_mqtt_module +from home.mqtt import MqttNode, MqttModule, MqttPayload, add_mqtt_module +from home.mqtt.module.relay import MqttPowerStatusPayload +from home.mqtt.module.temphum import MqttTemphumDataPayload +from home.mqtt.module.diagnostics import InitialDiagnosticsPayload + config.load('pump_bot') mqtt: Optional[MqttNode] = None mqtt_relay_module: Optional[MqttModule] = None +time_format = '%d.%m.%Y, %H:%M:%S' + +watering_mcu_status = { + 'last_time': 0, + 'last_boot_time': 0, + 'relay_opened': False, + 'ambient_temp': 0.0, + 'ambient_rh': 0.0, +} bot.initialize() bot.lang.ru( @@ -31,7 +46,9 @@ bot.lang.ru( start_watering="Включить полив", stop_watering="Отключить полив", - status="Статус", + status="Статус насоса", + watering_status="Статус полива", + done="Готово 👌", user_action_notification='Пользователь <a href="tg://user?id=%d">%s</a> <b>%s</b> насос.', user_watering_notification='Пользователь <a href="tg://user?id=%d">%s</a> <b>%s</b> полив.', @@ -55,7 +72,9 @@ bot.lang.en( start_watering="Start watering", stop_watering="Stop watering", - status="Status", + status="Pump status", + watering_status="Watering status", + done="Done 👌", user_action_notification='User <a href="tg://user?id=%d">%s</a> turned the pump <b>%s</b>.', user_watering_notification='User <a href="tg://user?id=%d">%s</a> <b>%s</b> the watering.', @@ -153,27 +172,60 @@ def status(ctx: bot.Context) -> None: ) +def _get_timestamp_as_string(timestamp: int) -> str: + if timestamp != 0: + return datetime.fromtimestamp(timestamp).strftime(time_format) + else: + return 'unknown' + + +@bot.handler(message='watering_status') +def watering_status(ctx: bot.Context) -> None: + buf = f'last report time: <b>{_get_timestamp_as_string(watering_mcu_status["last_time"])}</b>\n' + if watering_mcu_status["last_boot_time"] != 0: + buf += f'boot time: <b>{_get_timestamp_as_string(watering_mcu_status["last_boot_time"])}</b>\n' + buf += 'relay opened: <b>' + ('yes' if watering_mcu_status['relay_opened'] else 'no') + '</b>\n' + buf += f'ambient temp & humidity: <b>{watering_mcu_status["ambient_temp"]} C, {watering_mcu_status["ambient_rh"]}%</b>' + ctx.reply(buf) + + @bot.defaultreplymarkup def markup(ctx: Optional[bot.Context]) -> Optional[ReplyKeyboardMarkup]: - buttons = [ - [ctx.lang('enable'), ctx.lang('disable')], - ] - + buttons = [] if ctx.user_id in config['bot']['silent_users']: buttons.append([ctx.lang('enable_silently'), ctx.lang('disable_silently')]) - - buttons.append([ctx.lang('start_watering'), ctx.lang('stop_watering')]) - buttons.append([ctx.lang('status')]) + buttons.append([ctx.lang('enable'), ctx.lang('disable'), ctx.lang('status')],) + buttons.append([ctx.lang('start_watering'), ctx.lang('stop_watering'), ctx.lang('watering_status')]) return ReplyKeyboardMarkup(buttons, one_time_keyboard=False) +def mqtt_payload_callback(payload: MqttPayload): + global watering_mcu_status + + watering_mcu_status['last_time'] = int(time()) + + if isinstance(payload, InitialDiagnosticsPayload): + watering_mcu_status['last_boot_time'] = int(time()) + + elif isinstance(payload, MqttTemphumDataPayload): + watering_mcu_status['ambient_temp'] = payload.temp + watering_mcu_status['ambient_rh'] = payload.rh + + elif isinstance(payload, MqttPowerStatusPayload): + watering_mcu_status['relay_opened'] = payload.opened + + if __name__ == '__main__': mqtt = MqttNode(node_id=config.get('mqtt_water_relay.node_id')) if is_development_mode(): add_mqtt_module(mqtt, 'diagnostics') + + mqtt_relay_module = add_mqtt_module(mqtt, 'temphum') mqtt_relay_module = add_mqtt_module(mqtt, 'relay') + mqtt.add_payload_callback(mqtt_payload_callback) + mqtt.configure_tls() mqtt.connect_and_loop(loop_forever=False) |