diff --git a/assets/air_fryer_cookbook.png b/assets/air_fryer_cookbook.png new file mode 100644 index 0000000..fb2bfbe Binary files /dev/null and b/assets/air_fryer_cookbook.png differ diff --git a/assets/restart_button.png b/assets/restart_button.png new file mode 100644 index 0000000..0a447fd Binary files /dev/null and b/assets/restart_button.png differ diff --git a/berry/haco.be b/berry/haco.be index e53f1af..8832484 100644 --- a/berry/haco.be +++ b/berry/haco.be @@ -40,7 +40,7 @@ class ConfigDownloader if self.id_downloading && self.id_downloading==id - logger.error(string.format("Got duplicate new config (same as currently being download) ID: %s. Will be ignored.",id)) + logger.error(string.format("Got duplicate new config (same as currently being downloaded) ID: %s. Will be ignored.",id)) return end diff --git a/haco/__init__.py b/haco/__init__.py index e69de29..825ad5f 100644 --- a/haco/__init__.py +++ b/haco/__init__.py @@ -0,0 +1,17 @@ +from haco import tuya +from .button import Button +from .number import Number +from .pulldown import Select +from .sensor import Sensor +from .switch import Switch +from .tasmota import Tasmota + +__all__ = [ + "Button", + "Select", + "Sensor", + "Tasmota", + "Number", + "Switch", + "tuya" +] diff --git a/haco/configs/air_fryer.py b/haco/configs/air_fryer.py index 087d615..4999d4b 100644 --- a/haco/configs/air_fryer.py +++ b/haco/configs/air_fryer.py @@ -1,14 +1,11 @@ import math -from haco import tuya -from haco.pulldown import Pulldown -from haco.sensor import Sensor -from haco.tasmota import Tasmota +from haco import Tasmota, Number, Sensor, Select, Switch, tuya from haco.tools import add_tuya_io OPTIONS = ['Default', 'Fries', 'Shrimp', 'Pizza', 'Chicken', 'Fish', 'Steak', 'Cake', 'Bacon', 'Preheat', 'Custom'] -cookbook = Pulldown('Cookbook', options=OPTIONS, icon='mdi:chef-hat') +cookbook = Select('Cookbook', options=OPTIONS, icon='mdi:chef-hat') @cookbook.callback() @@ -34,11 +31,6 @@ def tasmota(value: int): return value -from haco.switch import Switch - -from haco.number import Number -from haco import tuya - cooking_time = Number( name='Cooking Time', number_range=range(1, 60), diff --git a/haco/configs/demo.py b/haco/configs/demo.py new file mode 100644 index 0000000..23731df --- /dev/null +++ b/haco/configs/demo.py @@ -0,0 +1,8 @@ +from haco import Button, Tasmota + +restart = Button('Restart Tasmota', icon='mdi:restart') + + +@restart.callback() +def ha(value) -> Tasmota[str, 'tasmota.cmd("restart 1")']: + return value diff --git a/haco/configs/development.py b/haco/configs/development.py index f48a507..4fd0197 100644 --- a/haco/configs/development.py +++ b/haco/configs/development.py @@ -1,6 +1,6 @@ from haco.climate import Climate from haco.number import Number -from haco.pulldown import Pulldown +from haco.pulldown import Select from haco.sensor import Sensor from haco.tasmota import Tasmota from haco.text import Text, Password @@ -123,7 +123,7 @@ def temperature_low_tasmota(value: int): return value -pulldown = Pulldown( +pulldown = Select( 'Dev Select Test Control', options=list('ABC') ) diff --git a/haco/device.py b/haco/device.py index 0d2d161..a68ecaf 100644 --- a/haco/device.py +++ b/haco/device.py @@ -9,8 +9,9 @@ from haco.tools import log_publish, split_into_chunks, log_subscribe class Device: - def __init__(self, mac, controls: List[Control], run_config_post=None): + def __init__(self, mac, config_module, controls: List[Control], run_config_post=None): self.name = None + self.config_module = config_module self.topic = None self.hostname = None self.mac = mac @@ -103,7 +104,8 @@ class Device: await log_subscribe(client, str(topic)) async def bind(self, client, data_announce): - print(f'Found match for self: {self}') + msg = f'Found device with matching config {self.config_module.__name__}: Hostname: {data_announce["hostname"]} MAC:{self.mac}' + tools.logger.info(msg) self.name = data_announce['device_name'] self.topic = data_announce['topic'] self.hostname = data_announce['hostname'] diff --git a/haco/load_configs.py b/haco/load_configs.py index c727cd6..f370bdb 100644 --- a/haco/load_configs.py +++ b/haco/load_configs.py @@ -52,6 +52,8 @@ def load_controls_from_directory(): module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) + args['config_module'] = module + for obj_name in dir(module): obj = getattr(module, obj_name) if obj_name == 'DEVICE_CONFIG': diff --git a/haco/mqtt.py b/haco/mqtt.py index ecb2110..4d0cf4f 100644 --- a/haco/mqtt.py +++ b/haco/mqtt.py @@ -1,11 +1,10 @@ import aiomqtt import asyncio import json -import logging import os from pathlib import Path -from haco import constants +from haco import constants, tools from haco.device import Device from haco.load_configs import load_devices from haco.tools import log_received @@ -27,25 +26,27 @@ async def listen(client, devices): if message.topic.matches(TOPIC_ANNOUNCE): if not message.payload: - logging.warning(f'Topic {message.topic.value} got empty payload. It may have been deleted.') + msg = f'Topic {message.topic.value} got empty payload. It may have been deleted.' + tools.logger.info(msg) continue data_announce = json.loads(message.payload) mac = data_announce['wifi']['mac'] if mac not in devices: - print(f'Got announce from device with no config: {data_announce}') + msg = f'Got announce from device with no config: Hostname: {data_announce["hostname"]} MAC:{data_announce["mac"]}. Publishing config data.' + tools.logger.info(msg) continue device: Device = devices[mac] if device.config_id and data_announce['config_id'] == device.config_id: - print(f'Device {device.name} ({device.hostname}:{device.mac}) successfully configured.') + msg = f'Device {device.name} successfully configured. Hostname: {device.hostname} MAC:{device.mac}' + tools.logger.info(msg) continue await device.bind(client, data_announce) devices[device.hostname] = device - device else: _, _, device_name, _, control, capability_id, platform, io = Path(message.topic.value).parts diff --git a/haco/pulldown.py b/haco/pulldown.py index b5276ee..5c51951 100644 --- a/haco/pulldown.py +++ b/haco/pulldown.py @@ -3,7 +3,7 @@ from typing import List from haco.control import Control -class Pulldown(Control): +class Select(Control): NAME = 'select' def __init__(self, name, options: List[str], icon=None):