Convert UpCloud to config flow, improve error handling (#37941)

This commit is contained in:
Ville Skyttä authored and GitHub committed 2020-10-15 23:26:01 +02:00
1 parent d790de1346
commit a9f342ef74
12 files changed
+479 -99

No files matched your search

+7 -14
View File
@@ -1,31 +1,24 @@
"""Support for interacting with UpCloud servers."""
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.const import STATE_OFF
from homeassistant.const import CONF_USERNAME, STATE_OFF
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
from . import CONF_SERVERS, DATA_UPCLOUD, SIGNAL_UPDATE_UPCLOUD, UpCloudServerEntity
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_SERVERS): vol.All(cv.ensure_list, [cv.string])}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the UpCloud server switch."""
upcloud = hass.data[DATA_UPCLOUD]
servers = config.get(CONF_SERVERS)
devices = [UpCloudSwitch(upcloud, uuid) for uuid in servers]
add_entities(devices, True)
coordinator = hass.data[DATA_UPCLOUD].coordinators[config_entry.data[CONF_USERNAME]]
entities = [UpCloudSwitch(coordinator, uuid) for uuid in coordinator.data]
async_add_entities(entities, True)
class UpCloudSwitch(UpCloudServerEntity, SwitchEntity):
@@ -34,10 +27,10 @@ class UpCloudSwitch(UpCloudServerEntity, SwitchEntity):
def turn_on(self, **kwargs):
"""Start the server."""
if self.state == STATE_OFF:
self.data.start()
self._server.start()
dispatcher_send(self.hass, SIGNAL_UPDATE_UPCLOUD)
def turn_off(self, **kwargs):
"""Stop the server."""
if self.is_on:
self.data.stop()
self._server.stop()