Skip to content

Commit

Permalink
Refine imports, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Aug 30, 2023
1 parent ab4540a commit b4bc004
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 22 deletions.
Binary file added assets/air_fryer_cookbook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/restart_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion berry/haco.be
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions haco/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
]
12 changes: 2 additions & 10 deletions haco/configs/air_fryer.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions haco/configs/demo.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions haco/configs/development.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -123,7 +123,7 @@ def temperature_low_tasmota(value: int):
return value


pulldown = Pulldown(
pulldown = Select(
'Dev Select Test Control',
options=list('ABC')
)
Expand Down
6 changes: 4 additions & 2 deletions haco/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

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
Expand Down Expand Up @@ -103,7 +104,8 @@ async def do_subscriptions(self, client):
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']
Expand Down
2 changes: 2 additions & 0 deletions haco/load_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
13 changes: 7 additions & 6 deletions haco/mqtt.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion haco/pulldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from haco.control import Control


class Pulldown(Control):
class Select(Control):
NAME = 'select'

def __init__(self, name, options: List[str], icon=None):
Expand Down

0 comments on commit b4bc004

Please sign in to comment.