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

Update iCloud sensors when service finish its update #30680

Merged
merged 2 commits into from
Jan 11, 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
4 changes: 2 additions & 2 deletions homeassistant/components/icloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
DEVICE_STATUS_SET,
DOMAIN,
ICLOUD_COMPONENTS,
SERVICE_UPDATE,
STORAGE_KEY,
STORAGE_VERSION,
TRACKER_UPDATE,
)

ATTRIBUTION = "Data provided by Apple iCloud"
Expand Down Expand Up @@ -336,7 +336,7 @@ def update_devices(self) -> None:
self._devices[device_id] = IcloudDevice(self, device, status)
self._devices[device_id].update(status)

dispatcher_send(self.hass, TRACKER_UPDATE)
dispatcher_send(self.hass, SERVICE_UPDATE)
self._fetch_interval = self._determine_interval()
track_point_in_utc_time(
self.hass,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/icloud/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""iCloud component constants."""

DOMAIN = "icloud"
TRACKER_UPDATE = f"{DOMAIN}_tracker_update"
SERVICE_UPDATE = f"{DOMAIN}_update"
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

CONF_ACCOUNT_NAME = "account_name"
CONF_MAX_INTERVAL = "max_interval"
Expand Down
14 changes: 7 additions & 7 deletions homeassistant/components/icloud/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DEVICE_LOCATION_LATITUDE,
DEVICE_LOCATION_LONGITUDE,
DOMAIN,
TRACKER_UPDATE,
SERVICE_UPDATE,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -77,11 +77,6 @@ def longitude(self):
"""Return longitude value of the device."""
return self._device.location[DEVICE_LOCATION_LONGITUDE]

@property
def should_poll(self) -> bool:
"""No polling needed."""
return False

@property
def battery_level(self) -> int:
"""Return the battery level of the device."""
Expand Down Expand Up @@ -112,10 +107,15 @@ def device_info(self) -> Dict[str, any]:
"model": self._device.device_model,
}

@property
def should_poll(self) -> bool:
"""No polling needed."""
return False

async def async_added_to_hass(self):
"""Register state update callback."""
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, TRACKER_UPDATE, self.async_write_ha_state
self.hass, SERVICE_UPDATE, self.async_write_ha_state
)

async def async_will_remove_from_hass(self):
Expand Down
19 changes: 18 additions & 1 deletion homeassistant/components/icloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME, DEVICE_CLASS_BATTERY
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.icon import icon_for_battery_level
from homeassistant.helpers.typing import HomeAssistantType

from . import IcloudDevice
from .const import DOMAIN
from .const import DOMAIN, SERVICE_UPDATE

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,6 +36,7 @@ class IcloudDeviceBatterySensor(Entity):
def __init__(self, device: IcloudDevice):
"""Initialize the battery sensor."""
self._device = device
self._unsub_dispatcher = None

@property
def unique_id(self) -> str:
Expand Down Expand Up @@ -83,3 +85,18 @@ def device_info(self) -> Dict[str, any]:
"manufacturer": "Apple",
"model": self._device.device_model,
}

@property
def should_poll(self) -> bool:
"""No polling needed."""
return False

async def async_added_to_hass(self):
"""Register state update callback."""
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, SERVICE_UPDATE, self.async_write_ha_state
)

async def async_will_remove_from_hass(self):
"""Clean up after entity before removal."""
self._unsub_dispatcher()