mirror of
https://github.com/fmtr/haco.git
synced 2026-10-25 18:40:15 -04:00
Add Control.DATA for static announce data
This commit is contained in:
parent
9fe9db547e
commit
70afdf1857
8 changed files with 40 additions and 25 deletions
|
|
@ -6,8 +6,10 @@ from typing import Any
|
|||
|
||||
@dataclass(kw_only=True)
|
||||
class Base:
|
||||
DATA = dict()
|
||||
|
||||
def model_dump(self) -> dict[str, Any]:
|
||||
data = {}
|
||||
data = {} | self.DATA
|
||||
for fld in fields(self):
|
||||
meta = fld.metadata
|
||||
if meta.get('exclude', False):
|
||||
|
|
|
|||
|
|
@ -1,15 +1,20 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Literal
|
||||
|
||||
from control import Control
|
||||
from haco.capabilities import Capability
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Button(Control):
|
||||
platform: Literal["climate"] = "button"
|
||||
DATA = dict(
|
||||
platform='button'
|
||||
)
|
||||
|
||||
def command(self, value):
|
||||
raise NotImplementedError()
|
||||
|
||||
def state(self, value):
|
||||
raise NotImplementedError()
|
||||
@classmethod
|
||||
def get_capabilities(cls):
|
||||
return [
|
||||
Capability(name=None, state=None)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import asyncio
|
||||
|
||||
import aiomqtt
|
||||
import asyncio
|
||||
|
||||
from fmtr.tools import mqtt, Constants
|
||||
from fmtr.tools.json_tools import to_json
|
||||
|
|
@ -40,7 +39,8 @@ class ClientHaco(mqtt.Client):
|
|||
|
||||
# Echo back as new state
|
||||
topic_state = topic_command.state
|
||||
await topic_state.wrap_back(echo_val)
|
||||
if topic_state:
|
||||
await topic_state.wrap_back(echo_val)
|
||||
|
||||
async def start(self):
|
||||
while True:
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ from control import Control
|
|||
|
||||
@dataclass(kw_only=True)
|
||||
class Climate(Control):
|
||||
platform: Literal["climate"] = "climate"
|
||||
DATA = dict(
|
||||
platform='climate'
|
||||
)
|
||||
|
||||
|
||||
temperature_unit: Literal["C", "F"] = "C"
|
||||
temperature_min: float = 0
|
||||
temperature_max: float = 40
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ if TYPE_CHECKING:
|
|||
@dataclass(kw_only=True)
|
||||
class Control(Base):
|
||||
name: str
|
||||
platform: str
|
||||
|
||||
|
||||
device: Device = field(default=None, init=False)
|
||||
capabilities: None | list[Capability] = field(default=None, metadata=dict(exclude=True))
|
||||
|
|
@ -69,7 +69,7 @@ class Control(Base):
|
|||
|
||||
@property
|
||||
def announce_topic(self):
|
||||
return f"homeassistant/{self.platform}/{self.unique_id}/config"
|
||||
return f"homeassistant/{self.DATA['platform']}/{self.unique_id}/config"
|
||||
|
||||
def get_availability_topic(self) -> str:
|
||||
return self.device.client.will.topic
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
from typing import List
|
||||
from dataclasses import dataclass
|
||||
|
||||
from haco.control import Control
|
||||
from control import Control
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Select(Control):
|
||||
NAME = 'select'
|
||||
DATA = dict(
|
||||
platform='select'
|
||||
)
|
||||
|
||||
def __init__(self, name, options: List[str], icon=None):
|
||||
self.options = options
|
||||
super().__init__(name, icon=icon)
|
||||
options: list[str]
|
||||
|
||||
def get_config_ha_ex(self):
|
||||
data = {
|
||||
'options': self.options,
|
||||
}
|
||||
def command(self, value):
|
||||
raise NotImplementedError()
|
||||
|
||||
return data
|
||||
def state(self, value):
|
||||
raise NotImplementedError()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from haco.client import ClientHaco
|
|||
from haco.climate import Climate
|
||||
from haco.constants import MQTT_HOST
|
||||
from haco.device import Device
|
||||
from haco.pulldown import Select
|
||||
from haco.switch import Switch
|
||||
|
||||
|
||||
|
|
@ -15,7 +16,8 @@ async def main():
|
|||
b1 = Button(name="Dev Button 1")
|
||||
|
||||
s1 = Switch(name="Dev Switch 1")
|
||||
device = Device(name="Dev Device", controls=[climate_a, climate_b, b1, s1])
|
||||
sel1 = Select(name="Dev Select 1", options=['a', 'b', 'c'])
|
||||
device = Device(name="Dev Device", controls=[climate_a, climate_b, b1, s1, sel1])
|
||||
|
||||
client = ClientHaco(hostname=MQTT_HOST, device=device)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Literal
|
||||
|
||||
from control import Control
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Switch(Control):
|
||||
platform: Literal["climate"] = "switch"
|
||||
DATA = dict(
|
||||
platform='switch'
|
||||
)
|
||||
|
||||
|
||||
def command(self, value):
|
||||
raise NotImplementedError()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue