mirror of
https://github.com/fmtr/haco.git
synced 2026-11-19 22:46:30 -05:00
Add topics property to Capability.
Some checks failed
Version and Deploy / build (push) Has been cancelled
Some checks failed
Version and Deploy / build (push) Has been cancelled
This commit is contained in:
parent
9c4d39f46b
commit
1bcf9fcd82
2 changed files with 18 additions and 10 deletions
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from fmtr.tools.iterator_tools import strip_none
|
||||
from haco.base import Base
|
||||
from haco.topics import AnnounceTopicState, AnnounceTopicCommand
|
||||
|
||||
|
|
@ -23,7 +24,7 @@ class Capability(Base):
|
|||
|
||||
def set_parent(self, control):
|
||||
self.parent = control
|
||||
for topic in (t for t in (self.state, self.command) if t):
|
||||
for topic in self.topics:
|
||||
topic.set_parent(self)
|
||||
|
||||
self.subscriptions = self.get_subscriptions()
|
||||
|
|
@ -32,16 +33,20 @@ class Capability(Base):
|
|||
|
||||
def get_announce(self) -> dict:
|
||||
data = {}
|
||||
for topic in (t for t in (self.state, self.command) if t):
|
||||
for topic in self.topics:
|
||||
data |= topic.announce
|
||||
return data
|
||||
|
||||
def get_subscriptions(self) -> dict:
|
||||
data = {}
|
||||
for topic in self.topics:
|
||||
data |= topic.subscriptions
|
||||
return data
|
||||
|
||||
@property
|
||||
def topics(self):
|
||||
return strip_none(self.state, self.command)
|
||||
|
||||
@property
|
||||
def control(self):
|
||||
return self.parent
|
||||
|
||||
def get_subscriptions(self) -> dict:
|
||||
data = {}
|
||||
for topic in (t for t in (self.state, self.command) if t):
|
||||
data |= topic.subscriptions
|
||||
return data
|
||||
|
|
|
|||
|
|
@ -9,10 +9,13 @@ from haco.obs import logger
|
|||
|
||||
|
||||
class ClientHaco(mqtt.Client):
|
||||
ONLINE = 'online'
|
||||
OFFLINE = 'offline'
|
||||
|
||||
aiomqtt.Client
|
||||
|
||||
def __init__(self, *args, device=None, will=None, identifier=None, **kwargs):
|
||||
self.will = will or mqtt.Will(topic=str(constants.TOPIC_AVAIL), payload="offline", retain=True, qos=1, )
|
||||
self.will = will or mqtt.Will(topic=str(constants.TOPIC_AVAIL), payload=self.OFFLINE, retain=True, qos=1, )
|
||||
identifier = identifier or constants.CLIENT_ID
|
||||
self.device = device
|
||||
self.device.set_parent(self)
|
||||
|
|
@ -49,7 +52,7 @@ class ClientHaco(mqtt.Client):
|
|||
logger.info(f'Announcing {topic} with {data_json}')
|
||||
await self.publish(topic, payload=data_json, retain=True)
|
||||
|
||||
await self.publish(self.will.topic, "online", retain=True)
|
||||
await self.publish(self.will.topic, self.ONLINE, retain=True)
|
||||
|
||||
# await self.publish('haco/development/3012edb1a6d4-1ed9c3469373/dev-device/dev-climate-b/power/command', "OFF", retain=True)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue