Skip to content

Commit

Permalink
Merge pull request #39 from Crewski/event-bus
Browse files Browse the repository at this point in the history
Send incoming messages to event bus
  • Loading branch information
Crewski authored Jul 7, 2023
2 parents 0791147 + a648fbe commit d8433e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ njsPC-HA communicates with nodejs-PoolController via the native API, the same wa
- Salt Target
- Salt Needed
- pH/Orp
- Schedules (toggle)
- Chem Controllers
- Filters

## Not Supported
- ?????
# EVENT BUS
If there is something in nodejs-PoolController that isn't in njsPC-HA, you can maninpulate the data yourself. All incoming data is sent to the Home Assistant event bus under the event `njspc-ha_event`. You can subscribe and view these events in the `EVENTS` tab in `Developer Tools`. The event topic is found in `data.evt` and the data is `data.data`. These are the same messages that Dashpanel receives which you can view them by using the developer console on your browser. If you find something you think should be added to this integration, just let us know.

# Home Assistant
![Home Assistant](/images/lovelace.png)
Expand Down
18 changes: 18 additions & 0 deletions custom_components/njspc_ha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,66 +132,79 @@ async def sio_connect(self):
async def handle_temps(data):
data["event"] = EVENT_TEMPS
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("pump")
async def handle_pump(data):
data["event"] = EVENT_PUMP
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("circuit")
async def handle_circuit(data):
data["event"] = EVENT_CIRCUIT
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("chlorinator")
async def handle_chlorinator(data):
data["event"] = EVENT_CHLORINATOR
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("chemController")
async def handle_chem_controller(data):
data["event"] = EVENT_CHEM_CONTROLLER
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("body")
async def handle_body(data):
data["event"] = EVENT_BODY
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("lightGroup")
async def handle_lightgroup(data):
data["event"] = EVENT_LIGHTGROUP
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("circuitGroup")
async def handle_circuitgroup(data):
data["event"] = EVENT_CIRCUITGROUP
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("feature")
async def handle_feature(data):
data["event"] = EVENT_FEATURE
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("controller")
async def handle_controller(data):
data["event"] = EVENT_CONTROLLER
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("filter")
async def handle_filter(data):
data["event"] = EVENT_FILTER
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("virtualCircuit")
async def handle_virtual_circuit(data):
data["event"] = EVENT_VIRTUAL_CIRCUIT
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.on("schedule")
async def handle_schedule(data):
data["event"] = EVENT_SCHEDULE
self.async_set_updated_data(data)
self.send_to_bus(data)

@self.sio.event
async def connect():
Expand Down Expand Up @@ -220,6 +233,11 @@ async def sio_close(self):
"""Close the connection to njsPC"""
await self.sio.disconnect()

def send_to_bus(self, data):
"""Send incoming messages to HA event bus"""
bus_data = {"evt": data["event"], "data": data}
self.hass.bus.async_fire("njspc-ha_event", bus_data)


class NjsPCHAapi:
"""API for sending data to nodejs-PoolController"""
Expand Down

0 comments on commit d8433e9

Please sign in to comment.