-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from timniklas/timniklas-patch-1
improved camera stream handling
- Loading branch information
Showing
7 changed files
with
227 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from dataclasses import dataclass | ||
from datetime import timedelta | ||
import logging | ||
|
||
import re | ||
import asyncio | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import ( | ||
CONF_DEVICE_ID, | ||
CONF_TOKEN | ||
) | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
||
from .const import DOMAIN | ||
from .mqtt import LifestyleMqtt | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
@dataclass | ||
class MqttData: | ||
"""Class to hold api data.""" | ||
|
||
connected: bool | ||
transmitting: bool | ||
|
||
class MqttCoordinator(DataUpdateCoordinator): | ||
"""My coordinator.""" | ||
|
||
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: | ||
"""Initialize coordinator.""" | ||
|
||
# Set variables from values entered in config flow setup | ||
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.transmitting = False | ||
|
||
# Initialise DataUpdateCoordinator | ||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name=f"{DOMAIN} ({config_entry.unique_id})", | ||
# Set update method to get devices on first load. | ||
update_method=self.async_update_data, | ||
# Do not set a polling interval as data will be pushed. | ||
# You can remove this line but left here for explanatory purposes. | ||
update_interval=None, | ||
) | ||
|
||
def updateData(self): | ||
self.async_set_updated_data(MqttData(self.mqtt_client.connected, 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. | ||
This is the place to pre-process the data to lookup tables | ||
so entities can quickly look up their data. | ||
""" | ||
if self.mqtt_client.connected == False: | ||
self.mqtt_client.connect() | ||
|
||
return MqttData(self.mqtt_client.connected, self.transmitting) | ||
|
||
async def call_door(self, duration: int = 60): | ||
await self.mqtt_client.call_door(duration) | ||
await self._transmit(duration) | ||
|
||
async def open_door(self): | ||
await self.mqtt_client.open_door() | ||
await self._updateTransmission(False) | ||
|
||
async def hangup_door(self): | ||
await self.mqtt_client.hangup_door() | ||
await self._updateTransmission(False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.