mirror of
https://github.com/fmtr/haco.git
synced 2026-11-25 01:16:28 -05:00
Fix Device computed fields order
This commit is contained in:
parent
a3e98abde3
commit
a568e35d0b
3 changed files with 35 additions and 26 deletions
|
|
@ -21,11 +21,11 @@ class Control(Base):
|
|||
announce: dict | None = field(default=None, metadata=dict(exclude=True))
|
||||
|
||||
def __post_init__(self):
|
||||
self.caps = self.get_capabilities()
|
||||
self.capabilities = self.get_capabilities()
|
||||
|
||||
def set_parent(self, device):
|
||||
self.device = device
|
||||
for cap in self.caps:
|
||||
for cap in self.capabilities:
|
||||
cap.set_parent(self)
|
||||
self.announce = self.get_announce()
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ class Control(Base):
|
|||
|
||||
def get_announce(self):
|
||||
data = self.model_dump()
|
||||
for cap in self.caps:
|
||||
for cap in self.capabilities:
|
||||
data |= cap.announce
|
||||
|
||||
data = {self.announce_topic: data}
|
||||
|
|
@ -45,7 +45,10 @@ class Control(Base):
|
|||
|
||||
@classmethod
|
||||
def get_capabilities(cls):
|
||||
raise NotImplementedError()
|
||||
return [
|
||||
Capability(name='default')
|
||||
]
|
||||
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
|
|
@ -76,3 +79,8 @@ class Control(Base):
|
|||
data |= capability.subscriptions
|
||||
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
control = Control(name="test", platform="test")
|
||||
control
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ from dataclasses import dataclass, field
|
|||
from functools import cached_property
|
||||
from typing import List, TYPE_CHECKING
|
||||
|
||||
from pydantic import computed_field
|
||||
|
||||
from haco.base import Base
|
||||
from haco.control import Control
|
||||
from haco.utils import sanitize_name
|
||||
|
|
@ -20,16 +18,26 @@ class Device(Base):
|
|||
manufacturer: str = "Demo"
|
||||
model: str = "Python MQTT Example"
|
||||
|
||||
identifiers: list[str] | None = field(default=None, init=False)
|
||||
|
||||
controls: List[Control] = field(default_factory=list, metadata=dict(exclude=True))
|
||||
parent: ClientHaco | None = field(metadata=dict(exclude=True))
|
||||
announce: dict | None = field(default=None, metadata=dict(exclude=True))
|
||||
|
||||
# def __post_init__(self):
|
||||
# self.identifiers=['ident']
|
||||
|
||||
def set_parent(self, client):
|
||||
self.parent = client
|
||||
|
||||
self.identifiers = [self.name_san]
|
||||
|
||||
for control in self.controls:
|
||||
control.set_parent(self)
|
||||
self.announce = self.get_announce()
|
||||
|
||||
self
|
||||
|
||||
@cached_property
|
||||
def topic(self):
|
||||
return self.client.topic / self.name_san
|
||||
|
|
@ -38,18 +46,8 @@ class Device(Base):
|
|||
def name_san(self) -> str:
|
||||
return sanitize_name(self.name)
|
||||
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def identifiers(self) -> list[str]:
|
||||
return [self.name_san]
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def connections(self) -> list[list[str]]:
|
||||
return [["mac", "AA:BB:CC:DD:EE:FF"]]
|
||||
|
||||
def get_announce(self):
|
||||
|
||||
data = {}
|
||||
for capability in self.controls:
|
||||
data |= capability.announce
|
||||
|
|
@ -66,3 +64,10 @@ class Device(Base):
|
|||
data |= control.subscriptions
|
||||
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
control = Control(name="test", platform="test")
|
||||
device = Device(name="test", parent=None, controls=[control])
|
||||
data = device.model_dump()
|
||||
print(device.topic)
|
||||
|
|
|
|||
14
haco/sb3.py
14
haco/sb3.py
|
|
@ -1,20 +1,16 @@
|
|||
from haco.client import ClientHaco
|
||||
from haco.climate import Climate
|
||||
from haco.constants import MQTT_HOST
|
||||
from haco.control import Control
|
||||
from haco.device import Device
|
||||
|
||||
control = Control(name="test", platform="test")
|
||||
device = Device(name="test", parent=None, controls=[control])
|
||||
|
||||
client = ClientHaco(
|
||||
|
||||
hostname=MQTT_HOST,
|
||||
|
||||
device=Device(
|
||||
parent=None,
|
||||
name='dev-device',
|
||||
controls=[
|
||||
Climate(name='Dev Climate A', platform='climate'),
|
||||
Climate(name='Dev Climate B', platform='climate'),
|
||||
]
|
||||
)
|
||||
device=device
|
||||
)
|
||||
|
||||
client
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue