mirror of
https://github.com/home-assistant/core
synced 2024-08-31 05:57:13 +02:00
Switch ios to dispatching instead of polling (#43233)
This commit is contained in:
parent
085aa3c99d
commit
810561e313
@ -9,6 +9,7 @@ from homeassistant.const import HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv, discovery
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.util.json import load_json, save_json
|
||||
|
||||
from .const import (
|
||||
@ -346,9 +347,11 @@ class iOSIdentifyDeviceView(HomeAssistantView):
|
||||
|
||||
data[ATTR_LAST_SEEN_AT] = datetime.datetime.now().isoformat()
|
||||
|
||||
name = data.get(ATTR_DEVICE_ID)
|
||||
device_id = data[ATTR_DEVICE_ID]
|
||||
|
||||
hass.data[DOMAIN][ATTR_DEVICES][name] = data
|
||||
hass.data[DOMAIN][ATTR_DEVICES][device_id] = data
|
||||
|
||||
async_dispatcher_send(hass, f"{DOMAIN}.{device_id}", data)
|
||||
|
||||
try:
|
||||
save_json(self._config_path, hass.data[DOMAIN])
|
||||
|
@ -1,9 +1,13 @@
|
||||
"""Support for Home Assistant iOS app sensors."""
|
||||
from homeassistant.components import ios
|
||||
from homeassistant.const import PERCENTAGE
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.icon import icon_for_battery_level
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
SENSOR_TYPES = {
|
||||
"level": ["Battery Level", PERCENTAGE],
|
||||
"state": ["Battery State", None],
|
||||
@ -73,6 +77,11 @@ class IOSSensor(Entity):
|
||||
device_id = self._device[ios.ATTR_DEVICE_ID]
|
||||
return f"{self.type}_{device_id}"
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement this sensor expresses itself in."""
|
||||
@ -114,7 +123,17 @@ class IOSSensor(Entity):
|
||||
return icon_state
|
||||
return icon_for_battery_level(battery_level=battery_level, charging=charging)
|
||||
|
||||
async def async_update(self):
|
||||
@callback
|
||||
def _update(self, device):
|
||||
"""Get the latest state of the sensor."""
|
||||
self._device = ios.devices(self.hass).get(self._device_name)
|
||||
self._device = device
|
||||
self._state = self._device[ios.ATTR_BATTERY][self.type]
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Added to hass so need to register to dispatch."""
|
||||
self._state = self._device[ios.ATTR_BATTERY][self.type]
|
||||
device_id = self._device[ios.ATTR_DEVICE_ID]
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(self.hass, f"{DOMAIN}.{device_id}", self._update)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user