Skip to content

Commit

Permalink
Fix Tado unique mobile device dispatcher (home-assistant#107631)
Browse files Browse the repository at this point in the history
* Add unique home ID device dispatch

* Adding fixture for new setup

* Minor refactor work

* Add check for unlinked to different homes

* If the interface returns an error

* Proper error handling

* Feedback fixes

* Comments for error in client

* Typo

* Update homeassistant/components/tado/__init__.py

Co-authored-by: Martin Hjelmare <[email protected]>

* Update homeassistant/components/tado/__init__.py

Co-authored-by: Martin Hjelmare <[email protected]>

* Update devices fix standard

* Dispatch out of loop

* Update dispatcher

* Clean up

---------

Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
2 people authored and frenck committed Jan 12, 2024
1 parent 644a823 commit f1fc5ab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
43 changes: 36 additions & 7 deletions homeassistant/components/tado/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ def setup(self):

def get_mobile_devices(self):
"""Return the Tado mobile devices."""
return self.tado.getMobileDevices()
return self.tado.get_mobile_devices()

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Update the registered zones."""
self.update_devices()
self.update_mobile_devices()
self.update_zones()
self.update_home()

Expand All @@ -203,17 +204,31 @@ def update_mobile_devices(self) -> None:
_LOGGER.error("Unable to connect to Tado while updating mobile devices")
return

if not mobile_devices:
_LOGGER.debug("No linked mobile devices found for home ID %s", self.home_id)
return

# Errors are planned to be converted to exceptions
# in PyTado library, so this can be removed
if "errors" in mobile_devices and mobile_devices["errors"]:
_LOGGER.error(
"Error for home ID %s while updating mobile devices: %s",
self.home_id,
mobile_devices["errors"],
)
return

for mobile_device in mobile_devices:
self.data["mobile_device"][mobile_device["id"]] = mobile_device
_LOGGER.debug(
"Dispatching update to %s mobile device: %s",
self.home_id,
mobile_device,
)

_LOGGER.debug(
"Dispatching update to %s mobile devices: %s",
self.home_id,
mobile_devices,
)
dispatcher_send(
self.hass,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED.format(self.home_id),
)

def update_devices(self):
Expand All @@ -224,6 +239,20 @@ def update_devices(self):
_LOGGER.error("Unable to connect to Tado while updating devices")
return

if not devices:
_LOGGER.debug("No linked devices found for home ID %s", self.home_id)
return

# Errors are planned to be converted to exceptions
# in PyTado library, so this can be removed
if "errors" in devices and devices["errors"]:
_LOGGER.error(
"Error for home ID %s while updating devices: %s",
self.home_id,
devices["errors"],
)
return

for device in devices:
device_short_serial_no = device["shortSerialNo"]
_LOGGER.debug("Updating device %s", device_short_serial_no)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/tado/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
DOMAIN = "tado"

SIGNAL_TADO_UPDATE_RECEIVED = "tado_update_received_{}_{}_{}"
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED = "tado_mobile_device_update_received"
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED = "tado_mobile_device_update_received_{}"
UNIQUE_ID = "unique_id"

DEFAULT_NAME = "Tado"
Expand Down
12 changes: 6 additions & 6 deletions homeassistant/components/tado/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import logging
from typing import Any

import voluptuous as vol

Expand All @@ -22,6 +21,7 @@
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType

from . import TadoConnector
from .const import CONF_HOME_ID, DATA, DOMAIN, SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -90,7 +90,7 @@ def update_devices() -> None:
entry.async_on_unload(
async_dispatcher_connect(
hass,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED.format(tado.home_id),
update_devices,
)
)
Expand All @@ -99,12 +99,12 @@ def update_devices() -> None:
@callback
def add_tracked_entities(
hass: HomeAssistant,
tado: Any,
tado: TadoConnector,
async_add_entities: AddEntitiesCallback,
tracked: set[str],
) -> None:
"""Add new tracker entities from Tado."""
_LOGGER.debug("Fetching Tado devices from API")
_LOGGER.debug("Fetching Tado devices from API for (newly) tracked entities")
new_tracked = []
for device_key, device in tado.data["mobile_device"].items():
if device_key in tracked:
Expand All @@ -128,7 +128,7 @@ def __init__(
self,
device_id: str,
device_name: str,
tado: Any,
tado: TadoConnector,
) -> None:
"""Initialize a Tado Device Tracker entity."""
super().__init__()
Expand Down Expand Up @@ -169,7 +169,7 @@ async def async_added_to_hass(self) -> None:
self.async_on_remove(
async_dispatcher_connect(
self.hass,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED,
SIGNAL_TADO_MOBILE_DEVICE_UPDATE_RECEIVED.format(self._tado.home_id),
self.on_demand_update,
)
)
Expand Down
26 changes: 26 additions & 0 deletions tests/components/tado/fixtures/mobile_devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"name": "Home",
"id": 123456,
"settings": {
"geoTrackingEnabled": false,
"specialOffersEnabled": false,
"onDemandLogRetrievalEnabled": false,
"pushNotifications": {
"lowBatteryReminder": true,
"awayModeReminder": true,
"homeModeReminder": true,
"openWindowReminder": true,
"energySavingsReportReminder": true,
"incidentDetection": true,
"energyIqReminder": false
}
},
"deviceMetadata": {
"platform": "Android",
"osVersion": "14",
"model": "Samsung",
"locale": "nl"
}
}
]
5 changes: 5 additions & 0 deletions tests/components/tado/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def async_init_integration(

token_fixture = "tado/token.json"
devices_fixture = "tado/devices.json"
mobile_devices_fixture = "tado/mobile_devices.json"
me_fixture = "tado/me.json"
weather_fixture = "tado/weather.json"
home_state_fixture = "tado/home_state.json"
Expand Down Expand Up @@ -70,6 +71,10 @@ async def async_init_integration(
"https://my.tado.com/api/v2/homes/1/devices",
text=load_fixture(devices_fixture),
)
m.get(
"https://my.tado.com/api/v2/homes/1/mobileDevices",
text=load_fixture(mobile_devices_fixture),
)
m.get(
"https://my.tado.com/api/v2/devices/WR1/",
text=load_fixture(device_wr1_fixture),
Expand Down

0 comments on commit f1fc5ab

Please sign in to comment.