Update iCloud sensors when service finish its update (#30680)

* Update iCloud sensors when needed

* Add sensor should_poll
This commit is contained in:
Quentame 2020-01-11 23:12:55 +01:00 committed by Martin Hjelmare
parent 62f53b656d
commit 519c1fa2da
4 changed files with 28 additions and 11 deletions

View File

@ -46,9 +46,9 @@ from .const import (
DEVICE_STATUS_SET,
DOMAIN,
ICLOUD_COMPONENTS,
SERVICE_UPDATE,
STORAGE_KEY,
STORAGE_VERSION,
TRACKER_UPDATE,
)
ATTRIBUTION = "Data provided by Apple iCloud"
@ -336,7 +336,7 @@ class IcloudAccount:
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,

View File

@ -1,7 +1,7 @@
"""iCloud component constants."""
DOMAIN = "icloud"
TRACKER_UPDATE = f"{DOMAIN}_tracker_update"
SERVICE_UPDATE = f"{DOMAIN}_update"
CONF_ACCOUNT_NAME = "account_name"
CONF_MAX_INTERVAL = "max_interval"

View File

@ -15,7 +15,7 @@ from .const import (
DEVICE_LOCATION_LATITUDE,
DEVICE_LOCATION_LONGITUDE,
DOMAIN,
TRACKER_UPDATE,
SERVICE_UPDATE,
)
_LOGGER = logging.getLogger(__name__)
@ -77,11 +77,6 @@ class IcloudTrackerEntity(TrackerEntity):
"""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."""
@ -112,10 +107,15 @@ class IcloudTrackerEntity(TrackerEntity):
"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):

View File

@ -4,12 +4,13 @@ from typing import Dict
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__)
@ -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:
@ -83,3 +85,18 @@ class IcloudDeviceBatterySensor(Entity):
"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()