I’m writing an integration for Biomatx, its a light control system. It’s made of hardware modules, a module holds 10 switches. Each switch toggle a light using a relay. The system is controlled by an RS485 serial connection where when a switch is pressed or released a 2 bytes packet is sent containing the module address (0 to 7), the switch address (0 to 9) and the switch state (released or pressed). I’m trying to design the API to control it to be used later in an integration and I was wondering how best to model this. The active part, toggling the switches is easy, I just need to write a packet to the serial so each switch writes to the bus. The passive part, where I need to read from the serial port to know when a switch has been pressed and update the state is where I’m stuck.
I have 3 modules, so in my integrations, I’ll have 30 entities. How am I supposed to update their state? Lets say I have an instance of this class:
class Bus: """This class is part of the API, not the integration""" async def toggle_switch(self, module_address, switch_address): packet = self.make_packet(module_address, switch_address) await self.serial_port.async_write(packet) async def read_event(self): # The protocol is 2 bytes per packet packet = await self.serial_port.async_read(2) module_address, switch_address = self.parse_packet(packet) return module_address, switch_address
Each entity can have a reference to that
Bus instance and call
toggle_switch in their
turn_on methods. So far so good. Now where do I call the
read_event method and how do I dispatch the state updates to my entities ?
Hope this is clear enough, any help welcome!