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

Strongly type the event types in DataUpdateCoordinator. #270

Merged
merged 4 commits into from
Sep 27, 2020
Merged
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
38 changes: 28 additions & 10 deletions custom_components/tahoma/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

from aiohttp import ServerDisconnectedError
from pyhoma.client import TahomaClient
from pyhoma.enums import EventName, ExecutionState
from pyhoma.exceptions import NotAuthenticatedException
from pyhoma.models import DataType, Device, State

from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

TYPES = {
Expand Down Expand Up @@ -55,9 +57,7 @@ async def _async_update_data(self) -> Dict[str, Device]:
_LOGGER.debug(exception)
self.executions = {}
await self.client.login()
self.devices = {
d.deviceurl: d for d in await self.client.get_devices(refresh=True)
}
self.devices = await self._get_devices()
return self.devices
except Exception as exception:
raise UpdateFailed(exception)
Expand All @@ -67,29 +67,43 @@ async def _async_update_data(self) -> Dict[str, Device]:
f"{event.name}/{event.exec_id} (device:{event.deviceurl},state:{event.old_state}->{event.new_state})"
)

if event.name == "DeviceAvailableEvent":
if event.name == EventName.DEVICE_AVAILABLE:
self.devices[event.deviceurl].available = True

elif event.name == "DeviceUnavailableEvent":
elif event.name in [
EventName.DEVICE_UNAVAILABLE,
EventName.DEVICE_DISABLED,
]:
self.devices[event.deviceurl].available = False

elif event.name == "DeviceStateChangedEvent":
elif event.name in [
EventName.DEVICE_CREATED,
EventName.DEVICE_UPDATED,
]:
self.devices = await self._get_devices()

elif event.name == EventName.DEVICE_REMOVED:
registry = await device_registry.async_get_registry(self.hass)
registry.async_remove_device(event.deviceurl)
del self.devices[event.deviceurl]

elif event.name == EventName.DEVICE_STATE_CHANGED:
for state in event.device_states:
device = self.devices[event.deviceurl]
if state.name not in device.states:
device.states[state.name] = state
device.states[state.name].value = self._get_state(state)

if event.name == "ExecutionRegisteredEvent":
elif event.name == EventName.EXECUTION_REGISTERED:
if event.exec_id not in self.executions:
self.executions[event.exec_id] = {}

self.update_interval = timedelta(seconds=1)

if (
event.name == "ExecutionStateChangedEvent"
elif (
event.name == EventName.EXECUTION_STATE_CHANGED
and event.exec_id in self.executions
and event.new_state in ["COMPLETED", "FAILED"]
and event.new_state in [ExecutionState.COMPLETED, ExecutionState.FAILED]
):
del self.executions[event.exec_id]

Expand All @@ -98,6 +112,10 @@ async def _async_update_data(self) -> Dict[str, Device]:

return self.devices

async def _get_devices(self) -> Dict[str, Device]:
"""Fetch devices."""
return {d.deviceurl: d for d in await self.client.get_devices(refresh=True)}

@staticmethod
def _get_state(state: State) -> Union[float, int, bool, str, None]:
"""Cast string value to the right type."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tahoma/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tahoma",
"requirements": [
"pyhoma==0.4.3"
"pyhoma==0.5.0"
],
"codeowners": ["@philklei", "@imicknl", "@vlebourl", "@tetienne"],
"issue_tracker": "https://github.com/imicknl/ha-tahoma/issues"
Expand Down
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pytest-cov<3.0.0
pytest-homeassistant

# from our manifest.json for our Custom Component
pyhoma==0.4.3
pyhoma==0.5.0