mirror of
https://github.com/home-assistant/core.git
synced 2025-08-06 14:15:12 +02:00
Use dispatcher
This commit is contained in:
@@ -15,6 +15,7 @@ import voluptuous as vol
|
||||
from homeassistant.const import __version__ as current_version
|
||||
from homeassistant.helpers import event
|
||||
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.util.dt as dt_util
|
||||
|
||||
@@ -28,6 +29,8 @@ CONF_COMPONENT_REPORTING = "include_used_components"
|
||||
|
||||
DOMAIN = "updater"
|
||||
|
||||
DISPATCHER_REMOTE_UPDATE = "updater_remote_update"
|
||||
|
||||
UPDATER_URL = "https://updater.home-assistant.io/"
|
||||
UPDATER_UUID_FILE = ".uuid"
|
||||
|
||||
@@ -49,9 +52,10 @@ RESPONSE_SCHEMA = vol.Schema(
|
||||
class Updater:
|
||||
"""Updater class."""
|
||||
|
||||
update_available = None
|
||||
release_notes = None
|
||||
newest_version = None
|
||||
def __init__(self, update_available: bool, newest_version: str, release_notes: str):
|
||||
self.update_available = update_available
|
||||
self.release_notes = release_notes
|
||||
self.newest_version = newest_version
|
||||
|
||||
|
||||
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)
|
||||
|
||||
updater = hass.data[DOMAIN] = Updater()
|
||||
|
||||
async def check_new_version(now):
|
||||
"""Check if a new version is available and report if one is."""
|
||||
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):
|
||||
_LOGGER.debug("Local version is newer than the latest version (%s)", newest)
|
||||
|
||||
updater.update_available = update_available
|
||||
updater.release_notes = release_notes
|
||||
updater.newest_version = newest
|
||||
updater = Updater(update_available, newest, release_notes)
|
||||
async_dispatcher_send(hass, DISPATCHER_REMOTE_UPDATE, updater)
|
||||
|
||||
# Update daily, start 1 hour after startup
|
||||
_dt = dt_util.utcnow() + timedelta(hours=1)
|
||||
|
@@ -1,23 +1,25 @@
|
||||
"""Support for Home Assistant Updater binary sensors."""
|
||||
|
||||
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):
|
||||
"""Set up the updater binary sensors."""
|
||||
updater = hass.data[DOMAIN]
|
||||
|
||||
async_add_entities([UpdaterBinary(updater)])
|
||||
async_add_entities([UpdaterBinary()])
|
||||
|
||||
|
||||
class UpdaterBinary(BinarySensorDevice):
|
||||
"""Representation of an updater binary sensor."""
|
||||
|
||||
def __init__(self, updater):
|
||||
def __init__(self):
|
||||
"""Initialize the binary sensor."""
|
||||
self._updater = updater
|
||||
self._update_available = None
|
||||
self._release_notes = None
|
||||
self._newest_version = None
|
||||
self._unsub_dispatcher = None
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@@ -32,22 +34,17 @@ class UpdaterBinary(BinarySensorDevice):
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the binary sensor is on."""
|
||||
return "on" if self._updater.update_available else "off"
|
||||
|
||||
@property
|
||||
def device_class(self) -> str:
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
return "connectivity"
|
||||
return self._update_available
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""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
|
||||
def should_poll(self) -> bool:
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def device_state_attributes(self) -> dict:
|
||||
@@ -55,8 +52,28 @@ class UpdaterBinary(BinarySensorDevice):
|
||||
data = super().device_state_attributes
|
||||
if data is None:
|
||||
data = {}
|
||||
if self._updater.release_notes:
|
||||
data[ATTR_RELEASE_NOTES] = self._updater.release_notes
|
||||
if self._updater.newest_version:
|
||||
data[ATTR_NEWEST_VERSION] = self._updater.newest_version
|
||||
if self._release_notes:
|
||||
data[ATTR_RELEASE_NOTES] = self._release_notes
|
||||
if self._newest_version:
|
||||
data[ATTR_NEWEST_VERSION] = self._newest_version
|
||||
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
|
||||
|
Reference in New Issue
Block a user