Skip to content

Commit ca3d794

Browse files
committed
Refine logging
1 parent 74f967f commit ca3d794

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

haco/client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
from fmtr.tools import mqtt, Constants
55
from haco import constants
6+
from haco.constants import SUBSCRIBE
67
from haco.obs import logger
8+
from haco.utils import get_prefix
79

810

911
class ClientHaco(mqtt.Client):
1012
ONLINE = 'online'
1113
OFFLINE = 'offline'
1214

13-
aiomqtt.Client
14-
1515
def __init__(self, *args, device=None, will=None, identifier=None, **kwargs):
1616
self.will = will or mqtt.Will(topic=str(constants.TOPIC_AVAIL), payload=self.OFFLINE, retain=True, qos=1, )
1717
identifier = identifier or constants.CLIENT_ID
@@ -24,10 +24,9 @@ def topic(self):
2424
return constants.TOPIC_CLIENT
2525

2626
async def handle(self):
27-
"""Listen for command messages from HA."""
2827

2928
for topic_sub in self.device.subscriptions.keys():
30-
logger.info(f"Subscribing to {topic_sub}")
29+
logger.info(f"{get_prefix(SUBSCRIBE)}: {topic_sub}")
3130
await self.subscribe(topic_sub)
3231

3332
async for message in self.messages:

haco/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
TOPIC_CLIENT = TOPIC_ROOT / CLIENT_ID
1414
TOPIC_AVAIL = TOPIC_CLIENT / STATUS
1515
PREFIX_MDI = "mdi:"
16+
17+
ANNOUNCE = 'announce'
18+
SUBSCRIBE = 'subscribe'

haco/control.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from dataclasses import dataclass, field
44
from typing import TYPE_CHECKING, TypeVar, Generic, Type, ClassVar
55

6+
from fmtr.tools import Constants
67
from fmtr.tools.json_tools import to_json
78
from haco.base import Base
89
from haco.capabilities import Capability
9-
from haco.constants import PREFIX_MDI
10+
from haco.constants import PREFIX_MDI, ANNOUNCE
1011
from haco.obs import logger
11-
from haco.utils import sanitize_name, Converters, ConvertersBool
12+
from haco.utils import sanitize_name, Converters, ConvertersBool, get_prefix
1213

1314
if TYPE_CHECKING:
1415
from haco.device import Device
@@ -27,7 +28,6 @@ class Control(Base, Generic[DeviceT]):
2728
unique_id: str | None = field(default=None, init=False)
2829
availability_topic: str | None = field(default=None, init=False)
2930

30-
# announce: dict | None = field(default=None, metadata=dict(exclude=True))
3131
subscriptions: dict | None = field(default=None, metadata=dict(exclude=True), init=False)
3232

3333
def __post_init__(self):
@@ -49,9 +49,6 @@ def set_parent(self, device):
4949
self.unique_id = self.get_unique_id()
5050
self.availability_topic = self.get_availability_topic()
5151
self.subscriptions = self.get_subscriptions()
52-
#self.announce = self.get_announce()
53-
54-
5552

5653
@property
5754
def parent(self):
@@ -111,8 +108,11 @@ async def announce(self):
111108
112109
"""
113110
topic = self.announce_topic
114-
data_json = to_json(self.get_announce())
115-
logger.info(f'Announcing {topic} with {data_json}')
111+
data = self.get_announce()
112+
data_json = to_json(data)
113+
114+
logger.info(f'{get_prefix(ANNOUNCE)}: {data} {Constants.ARROW_RIGHT} {topic}')
115+
116116
await self.device.client.publish(topic, data_json, retain=True)
117117

118118

haco/device.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ def set_parent(self, client):
3434

3535
for control in self.controls:
3636
control.set_parent(self)
37-
#self.announce = self.get_announce()
3837
self.subscriptions = self.get_subscriptions()
3938

40-
self
41-
4239
@cached_property
4340
def topic(self):
4441
return self.client.topic / self.name_san

haco/topics.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from functools import cached_property
55
from typing import ClassVar, TYPE_CHECKING, Callable
66

7-
from fmtr.tools import aio
7+
from fmtr.tools import aio, Constants
88
from haco.obs import logger
9+
from haco.utils import get_prefix
910

1011
if TYPE_CHECKING:
1112
from capabilities import Capability
@@ -107,6 +108,9 @@ async def handle(self, value=None):
107108

108109
value_raw = self.capability.converters.state(value)
109110
await self.capability.control.device.client.publish(self.topic, value_raw)
111+
112+
logger.info(f'{get_prefix(self.IO)}: {self.topic} {Constants.ARROW_RIGHT} {value_raw}')
113+
110114
return value_raw
111115

112116
@dataclass(kw_only=True)
@@ -119,14 +123,17 @@ def state(self):
119123

120124
async def handle(self, message):
121125

126+
value = message.payload.decode('utf-8')
127+
value = self.capability.converters.command(value)
128+
129+
logger.info(f'{get_prefix(self.IO)}: {value} {Constants.ARROW_LEFT} {self.topic}')
130+
131+
122132
if not self.control_method:
123133
logger.error(f'Incomplete base class: {self.callback_class_method_name}')
124134
return
125135

126136
is_async = aio.is_async(self.control_method)
127-
value = message.payload.decode('utf-8')
128-
value = self.capability.converters.command(value)
129-
130137
try:
131138

132139
if is_async:
@@ -138,7 +145,6 @@ async def handle(self, message):
138145
logger.warning(f'Subclass has not implemented method: {self.callback_class_method_name} {self.topic} {value}')
139146
return
140147

141-
# Echo back as new state
142148
topic_state = self.state
143149
if topic_state:
144150
await topic_state.handle(value_raw)

haco/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ class ConvertersNumeric(Converters):
5757
@dataclass
5858
class Metadata:
5959
exclude: bool = False
60+
61+
62+
def get_prefix(io: str) -> str:
63+
return io[:2].upper()

haco/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.4
1+
0.1.5

0 commit comments

Comments
 (0)