mirror of
https://github.com/fmtr/haco.git
synced 2026-11-20 15:06:28 -05:00
Allow hostnames as identifiers
This commit is contained in:
parent
9f2bc075fa
commit
a719aab68f
4 changed files with 41 additions and 38 deletions
|
|
@ -173,7 +173,7 @@ class Daemon
|
|||
|
||||
def init()
|
||||
|
||||
logger.info(string.format('Starting haco daemon in %s seconds...', self.DELAY_SUBSCRIBE))
|
||||
logger.info(string.format('Starting haco daemon (v%s) in %s seconds...',haco_version.VERSION, self.DELAY_SUBSCRIBE))
|
||||
|
||||
self.registry_daemon=tools.callbacks.Registry()
|
||||
self.registry_config=tools.callbacks.Registry()
|
||||
|
|
@ -187,7 +187,7 @@ class Daemon
|
|||
|
||||
def subscribe_daemon()
|
||||
|
||||
var rule_announce=tools.callbacks.Rule('mqtt#connected',/->print('announced called by mqtt#connected',self.publish_announce()))
|
||||
var rule_announce=tools.callbacks.Rule('mqtt#connected',/->self.publish_announce())
|
||||
self.registry_daemon.add(rule_announce)
|
||||
|
||||
var rule_config=tools.callbacks.MqttSubscription(
|
||||
|
|
@ -198,7 +198,7 @@ class Daemon
|
|||
|
||||
if mqtt.connected() self.publish_announce() end
|
||||
|
||||
logger.info(string.format('haco daemon has started.', self.DELAY_SUBSCRIBE))
|
||||
logger.info(string.format('The haco daemon has started. Hostname: %s, MAC: %s. Listening for configuration...', tasmota.hostname(), tools.network.get_mac()))
|
||||
|
||||
end
|
||||
|
||||
|
|
@ -222,9 +222,11 @@ class Daemon
|
|||
def get_data_announce()
|
||||
|
||||
return {
|
||||
'version': haco_version.VERSION,
|
||||
'hostname': tasmota.hostname(),
|
||||
'eth': tasmota.eth(),
|
||||
'wifi': tasmota.wifi(),
|
||||
'mac': tools.network.get_mac(),
|
||||
'timestamp': tasmota.rtc(),
|
||||
'config_id': self.config_id,
|
||||
'topic': tools.mqtt.get_topic(),
|
||||
|
|
|
|||
|
|
@ -9,12 +9,13 @@ from haco.tools import log_publish, split_into_chunks, log_subscribe
|
|||
|
||||
class Device:
|
||||
|
||||
def __init__(self, mac, config_module, controls: List[Control], run_config_post=None):
|
||||
def __init__(self, identifier, 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
|
||||
self.mac = None
|
||||
self.identifier = identifier
|
||||
self.run_config_post = run_config_post
|
||||
self.controls = controls
|
||||
self.config_id = None
|
||||
|
|
@ -109,6 +110,7 @@ class Device:
|
|||
self.name = data_announce['device_name']
|
||||
self.topic = data_announce['topic']
|
||||
self.hostname = data_announce['hostname']
|
||||
self.mac = data_announce['mac']
|
||||
|
||||
await self.do_subscriptions(client)
|
||||
|
||||
|
|
|
|||
|
|
@ -30,39 +30,32 @@ def load_controls_from_directory():
|
|||
for data in options['assignments']:
|
||||
|
||||
name = data['config']
|
||||
macs = data['identifier']
|
||||
identifier = data['identifier']
|
||||
|
||||
if type(macs) is not list:
|
||||
macs = [macs]
|
||||
args = {'identifier': identifier}
|
||||
|
||||
macs
|
||||
py_file = CONFIGS_PATH / Path(name).with_suffix('.py')
|
||||
module_name = py_file.stem
|
||||
|
||||
for mac in macs:
|
||||
try:
|
||||
module = importlib.import_module(f'haco.configs.{module_name}')
|
||||
reload(module)
|
||||
except ImportError:
|
||||
spec = importlib.util.spec_from_file_location(module_name, str(py_file))
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
args = {'mac': mac}
|
||||
args['config_module'] = module
|
||||
|
||||
py_file = CONFIGS_PATH / Path(name).with_suffix('.py')
|
||||
module_name = py_file.stem
|
||||
for obj_name in dir(module):
|
||||
obj = getattr(module, obj_name)
|
||||
if obj_name == 'DEVICE_CONFIG':
|
||||
args.update(obj)
|
||||
if isinstance(obj, Control):
|
||||
args.setdefault('controls', [])
|
||||
args['controls'].append(obj)
|
||||
|
||||
try:
|
||||
module = importlib.import_module(f'haco.configs.{module_name}')
|
||||
reload(module)
|
||||
except ImportError:
|
||||
spec = importlib.util.spec_from_file_location(module_name, str(py_file))
|
||||
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':
|
||||
args.update(obj)
|
||||
if isinstance(obj, Control):
|
||||
args.setdefault('controls', [])
|
||||
args['controls'].append(obj)
|
||||
|
||||
controls.append(args)
|
||||
controls.append(args)
|
||||
|
||||
return controls
|
||||
|
||||
|
|
|
|||
18
haco/mqtt.py
18
haco/mqtt.py
|
|
@ -31,16 +31,22 @@ async def listen(client, devices):
|
|||
continue
|
||||
|
||||
data_announce = json.loads(message.payload)
|
||||
mac = data_announce['wifi']['mac']
|
||||
if mac not in devices:
|
||||
|
||||
identifiers = {data_announce['wifi'].get('mac'), data_announce['eth'].get('mac'),
|
||||
data_announce['hostname'], data_announce['mac']}
|
||||
|
||||
for identifier in identifiers:
|
||||
device: Device = devices.get(identifier)
|
||||
if device:
|
||||
break
|
||||
|
||||
if not device:
|
||||
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:
|
||||
msg = f'Device {device.name} successfully configured. Hostname: {device.hostname} MAC:{device.mac}'
|
||||
msg = f'Device "{device.name}" successfully configured. Hostname: {device.hostname} MAC:{device.mac}'
|
||||
tools.logger.info(msg)
|
||||
continue
|
||||
|
||||
|
|
@ -56,7 +62,7 @@ async def listen(client, devices):
|
|||
|
||||
async def start_mqtt():
|
||||
devices = load_devices()
|
||||
devices = {device.mac: device for device in devices}
|
||||
devices = {device.identifier: device for device in devices}
|
||||
|
||||
async with aiomqtt.Client(hostname=MQTT_HOST, port=MQTT_PORT, username=MQTT_USERNAME,
|
||||
password=MQTT_PASSWORD) as client:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue