Refine imports, logging

This commit is contained in:
Edward Brown 2023-08-30 16:43:34 +01:00
parent ab4540aff4
commit b4bc004ac9
11 changed files with 44 additions and 22 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

BIN
assets/restart_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View file

@ -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

View file

@ -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"
]

View file

@ -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),

8
haco/configs/demo.py Normal file
View file

@ -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

View file

@ -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')
)

View file

@ -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']

View file

@ -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':

View file

@ -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

View file

@ -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):