Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request status after startup and handle binary status report response #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cbus/daemon/cmqttd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
async def create_serial_connection(*_, **__):
raise ImportError('Serial device support requires pyserial-asyncio')

from cbus.common import MIN_GROUP_ADDR, MAX_GROUP_ADDR, check_ga, Application
from cbus.common import MIN_GROUP_ADDR, MAX_GROUP_ADDR, check_ga, Application, GroupState
from cbus.paho_asyncio import AsyncioHelper
from cbus.protocol.pciprotocol import PCIProtocol
from cbus.protocol.cal.report import BinaryStatusReport
from cbus.toolkit.cbz import CBZ


Expand Down Expand Up @@ -111,7 +112,24 @@ def on_lighting_group_off(self, source_addr, group_addr):
if not self.mqtt_api:
return
self.mqtt_api.lighting_group_off(source_addr, group_addr)


def on_extended_cal(self, source_addr, extended_cal):
if not self.mqtt_api:
return
logger.debug(f'Block start: {extended_cal.block_start!r}')
if isinstance(extended_cal.report, BinaryStatusReport):
group_addr = extended_cal.block_start
for s in extended_cal.report:
if s == GroupState.ON:
logger.debug(f'group_addr {group_addr!r} is ON')
self.mqtt_api.lighting_group_on(source_addr, group_addr)
elif s == GroupState.OFF:
logger.debug(f'group_addr {group_addr!r} is OFF')
self.mqtt_api.lighting_group_off(source_addr, group_addr)
group_addr += 1
else:
logger.debug(f'unhandled report type {extended_cal.report!r}')

# TODO: on_lighting_group_terminate_ramp

def on_clock_request(self, source_addr):
Expand Down
34 changes: 33 additions & 1 deletion cbus/protocol/pciprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async def create_serial_connection(*_, **__):
from cbus.protocol.base_packet import (
BasePacket, SpecialServerPacket, SpecialClientPacket)
from cbus.protocol.cal.identify import IdentifyCAL
from cbus.protocol.cal.extended import ExtendedCAL
from cbus.protocol.cbus_protocol import CBusProtocol
from cbus.protocol.confirm_packet import ConfirmationPacket
from cbus.protocol.dm_packet import DeviceManagementPacket
Expand Down Expand Up @@ -88,11 +89,13 @@ def connection_made(self, transport: WriteTransport) -> None:

"""
self._transport = transport
self.pci_reset()
self.pci_reset()
create_task(self.req_status())
if self._timesync_frequency:
create_task(self.timesync())

def connection_lost(self, exc: Optional[Exception]) -> None:
logger.debug("Connection lost")
self._transport = None
self._connection_lost_future.set_result(True)

Expand Down Expand Up @@ -134,6 +137,12 @@ def handle_cbus_packet(self, p: BasePacket) -> None:
self.on_clock_update(p.source_address, s.val)
else:
logger.debug(f'hcp: unhandled SAL type: {s!r}')
elif isinstance(p, PointToPointPacket):
for s in p:
if isinstance(s, ExtendedCAL):
self.on_extended_cal(p.source_address, s)
else:
logger.debug(f'hcp: unhandled P2P type: {s!r}')
else:
logger.debug(f'hcp: unhandled other packet: {p!r}')

Expand Down Expand Up @@ -309,6 +318,18 @@ def on_clock_update(self, source_addr, val):

"""
logger.debug(f'recv: clock update from {source_addr} of {val!r}')

def on_extended_cal(self, source_addr, extended_cal):
"""
Event called when a unit sends a binary status report to the PCI.

:param source_addr: Source address of the unit requesting time.
:type source_addr: int
:param extended_cal: Extended CAL Object
:type extended_cal: ExtendedCAL

"""
logger.debug(f'recv: extended CAL from {source_addr} of {extended_cal}')

# other things.

Expand Down Expand Up @@ -411,6 +432,7 @@ def pci_reset(self):
self._send(DeviceManagementPacket(
checksum=False, parameter=0x30, value=0x59),
basic_mode=True)


def identify(self, unit_address, attribute):
"""
Expand Down Expand Up @@ -551,6 +573,16 @@ def clock_datetime(self, when: Optional[datetime] = None):
p = PointToMultipointPacket(sals=clock_update_sal(when))
return self._send(p)

async def req_status(self):

await sleep(5)

self._send(PointToMultipointPacket(sals=StatusRequestSAL(
child_application=Application.LIGHTING,
level_request=False,
group_address=0,
)))

async def timesync(self):
frequency = self._timesync_frequency
if frequency <= 0:
Expand Down