Skip to content

Commit

Permalink
Merge pull request #13 from timniklas/timniklas-patch-1
Browse files Browse the repository at this point in the history
added ha events
  • Loading branch information
timniklas authored Oct 31, 2024
2 parents 67887c5 + aedd486 commit 2b8f2cd
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions custom_components/ilifestyle/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
deviceid = config_entry.data[CONF_DEVICE_ID]
token = config_entry.data[CONF_TOKEN]

self.mqtt_client = LifestyleMqtt(mqtt_username=deviceid, mqtt_password=token, callback=self.updateData)
self._hass = hass
self.mqtt_client = LifestyleMqtt(mqtt_username=deviceid, mqtt_password=token, callback=self._updateConnection)
self.transmitting = False

# Initialise DataUpdateCoordinator
Expand All @@ -49,18 +50,24 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
# You can remove this line but left here for explanatory purposes.
update_interval=None,
)
def updateData(self):

def _updateData(self):
self.async_set_updated_data(MqttData(self.mqtt_client.connected, self.transmitting))

def _updateConnection(self):
self._updateData()
self._hass.bus.fire("ilifestyle_connection_event", {"connected": self.mqtt_client.connected})

async def _updateTransmission(self, state: bool):
if state != self.transmitting: #ignore double fires
self.transmitting = state
self._updateData()
self._hass.bus.fire("ilifestyle_transmission_event", {"transmission": self.transmitting})

async def _transmit(self, duration: int = 60):
await self._updateTransmission(True)
await asyncio.sleep(duration)
await self._updateTransmission(False)

async def _updateTransmission(self, state: bool):
self.transmitting = state
self.updateData()

async def async_update_data(self):
"""Fetch data from API endpoint.
Expand Down

0 comments on commit 2b8f2cd

Please sign in to comment.