UpCloud updates (#28646)

* Upgrade upcloud-api to 0.4.5

* Fix UpCloud name spelling in manifest

* Update data at setup time for better initial states

* Clean up signal handlers on remove

* Signal data update on server start

To keep related binary sensor better up to date.

* Improve track_time_interval emulation for initial update
pull/29135/head
Ville Skyttä 2019-11-27 12:17:14 +08:00 committed by Paulus Schoutsen
parent 2a7aac6afc
commit 738b3363d9
4 changed files with 19 additions and 6 deletions

View File

@ -18,6 +18,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_time_interval
from homeassistant.util import dt
_LOGGER = logging.getLogger(__name__)
@ -82,6 +83,7 @@ def setup(hass, config):
dispatcher_send(hass, SIGNAL_UPDATE_UPCLOUD)
# Call the UpCloud API to refresh data
upcloud_update(dt.utcnow())
track_time_interval(hass, upcloud_update, scan_interval)
return True
@ -108,6 +110,7 @@ class UpCloudServerEntity(Entity):
self._upcloud = upcloud
self.uuid = uuid
self.data = None
self._unsub_handlers = []
@property
def unique_id(self) -> str:
@ -124,10 +127,18 @@ class UpCloudServerEntity(Entity):
async def async_added_to_hass(self):
"""Register callbacks."""
async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_UPCLOUD, self._update_callback
self._unsub_handlers.append(
async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_UPCLOUD, self._update_callback
)
)
async def async_will_remove_from_hass(self) -> None:
"""Invoke unsubscription handlers."""
for unsub in self._unsub_handlers:
unsub()
self._unsub_handlers.clear()
@callback
def _update_callback(self):
"""Call update method."""

View File

@ -1,9 +1,9 @@
{
"domain": "upcloud",
"name": "Upcloud",
"name": "UpCloud",
"documentation": "https://www.home-assistant.io/integrations/upcloud",
"requirements": [
"upcloud-api==0.4.3"
"upcloud-api==0.4.5"
],
"dependencies": [],
"codeowners": [

View File

@ -6,8 +6,9 @@ import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import STATE_OFF
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
from . import CONF_SERVERS, DATA_UPCLOUD, UpCloudServerEntity
from . import CONF_SERVERS, DATA_UPCLOUD, SIGNAL_UPDATE_UPCLOUD, UpCloudServerEntity
_LOGGER = logging.getLogger(__name__)
@ -34,6 +35,7 @@ class UpCloudSwitch(UpCloudServerEntity, SwitchDevice):
"""Start the server."""
if self.state == STATE_OFF:
self.data.start()
dispatcher_send(self.hass, SIGNAL_UPDATE_UPCLOUD)
def turn_off(self, **kwargs):
"""Stop the server."""

View File

@ -1959,7 +1959,7 @@ twilio==6.32.0
unifiled==0.11
# homeassistant.components.upcloud
upcloud-api==0.4.3
upcloud-api==0.4.5
# homeassistant.components.huawei_lte
url-normalize==1.4.1