Use dispatcher

This commit is contained in:
Tobias Haase
2019-08-01 22:36:46 +02:00
parent b31bffe163
commit e1df96e95c
2 changed files with 44 additions and 26 deletions

View File

@@ -15,6 +15,7 @@ import voluptuous as vol
from homeassistant.const import __version__ as current_version from homeassistant.const import __version__ as current_version
from homeassistant.helpers import event from homeassistant.helpers import event
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import async_dispatcher_send
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@@ -28,6 +29,8 @@ CONF_COMPONENT_REPORTING = "include_used_components"
DOMAIN = "updater" DOMAIN = "updater"
DISPATCHER_REMOTE_UPDATE = "updater_remote_update"
UPDATER_URL = "https://updater.home-assistant.io/" UPDATER_URL = "https://updater.home-assistant.io/"
UPDATER_UUID_FILE = ".uuid" UPDATER_UUID_FILE = ".uuid"
@@ -49,9 +52,10 @@ RESPONSE_SCHEMA = vol.Schema(
class Updater: class Updater:
"""Updater class.""" """Updater class."""
update_available = None def __init__(self, update_available: bool, newest_version: str, release_notes: str):
release_notes = None self.update_available = update_available
newest_version = None self.release_notes = release_notes
self.newest_version = newest_version
def _create_uuid(hass, filename=UPDATER_UUID_FILE): def _create_uuid(hass, filename=UPDATER_UUID_FILE):
@@ -88,8 +92,6 @@ async def async_setup(hass, config):
include_components = config.get(CONF_COMPONENT_REPORTING) include_components = config.get(CONF_COMPONENT_REPORTING)
updater = hass.data[DOMAIN] = Updater()
async def check_new_version(now): async def check_new_version(now):
"""Check if a new version is available and report if one is.""" """Check if a new version is available and report if one is."""
result = await get_newest_version(hass, huuid, include_components) result = await get_newest_version(hass, huuid, include_components)
@@ -117,9 +119,8 @@ async def async_setup(hass, config):
elif StrictVersion(newest) < StrictVersion(current_version): elif StrictVersion(newest) < StrictVersion(current_version):
_LOGGER.debug("Local version is newer than the latest version (%s)", newest) _LOGGER.debug("Local version is newer than the latest version (%s)", newest)
updater.update_available = update_available updater = Updater(update_available, newest, release_notes)
updater.release_notes = release_notes async_dispatcher_send(hass, DISPATCHER_REMOTE_UPDATE, updater)
updater.newest_version = newest
# Update daily, start 1 hour after startup # Update daily, start 1 hour after startup
_dt = dt_util.utcnow() + timedelta(hours=1) _dt = dt_util.utcnow() + timedelta(hours=1)

View File

@@ -1,23 +1,25 @@
"""Support for Home Assistant Updater binary sensors.""" """Support for Home Assistant Updater binary sensors."""
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import ATTR_NEWEST_VERSION, ATTR_RELEASE_NOTES, DOMAIN from . import ATTR_NEWEST_VERSION, ATTR_RELEASE_NOTES, DISPATCHER_REMOTE_UPDATE, Updater
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the updater binary sensors.""" """Set up the updater binary sensors."""
updater = hass.data[DOMAIN] async_add_entities([UpdaterBinary()])
async_add_entities([UpdaterBinary(updater)])
class UpdaterBinary(BinarySensorDevice): class UpdaterBinary(BinarySensorDevice):
"""Representation of an updater binary sensor.""" """Representation of an updater binary sensor."""
def __init__(self, updater): def __init__(self):
"""Initialize the binary sensor.""" """Initialize the binary sensor."""
self._updater = updater self._update_available = None
self._release_notes = None
self._newest_version = None
self._unsub_dispatcher = None
@property @property
def name(self) -> str: def name(self) -> str:
@@ -32,22 +34,17 @@ class UpdaterBinary(BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return "on" if self._updater.update_available else "off" return self._update_available
@property
def device_class(self) -> str:
"""Return the class of this device, from component DEVICE_CLASSES."""
return "connectivity"
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
return True if self._updater.update_available is not None else False return self._update_available is not None
@property @property
def should_poll(self) -> bool: def should_poll(self) -> bool:
"""Return True if entity has to be polled for state.""" """Return True if entity has to be polled for state."""
return True return False
@property @property
def device_state_attributes(self) -> dict: def device_state_attributes(self) -> dict:
@@ -55,8 +52,28 @@ class UpdaterBinary(BinarySensorDevice):
data = super().device_state_attributes data = super().device_state_attributes
if data is None: if data is None:
data = {} data = {}
if self._updater.release_notes: if self._release_notes:
data[ATTR_RELEASE_NOTES] = self._updater.release_notes data[ATTR_RELEASE_NOTES] = self._release_notes
if self._updater.newest_version: if self._newest_version:
data[ATTR_NEWEST_VERSION] = self._updater.newest_version data[ATTR_NEWEST_VERSION] = self._newest_version
return data return data
async def async_added_to_hass(self):
"""Register update dispatcher."""
async def async_state_update(updater: Updater):
"""Update callback."""
self._newest_version = updater.newest_version
self._release_notes = updater.release_notes
self._update_available = updater.update_available
self.async_schedule_update_ha_state()
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update
)
async def async_will_remove_from_hass(self):
"""Register update dispatcher."""
if self._unsub_dispatcher is not None:
self._unsub_dispatcher()
self._unsub_dispatcher = None