2019-02-13 20:21:14 +00:00
|
|
|
"""Support for interacting with UpCloud servers."""
|
2018-02-28 21:17:12 +00:00
|
|
|
|
2021-05-15 04:49:41 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-12-16 14:14:46 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-05-15 04:49:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-10-15 21:26:01 +00:00
|
|
|
from homeassistant.const import CONF_USERNAME, STATE_OFF
|
2021-05-15 04:49:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-11-27 04:17:14 +00:00
|
|
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
2021-05-15 04:49:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-12-16 14:14:46 +00:00
|
|
|
from . import DATA_UPCLOUD, SIGNAL_UPDATE_UPCLOUD, UpCloudServerEntity
|
2018-02-28 21:17:12 +00:00
|
|
|
|
|
|
|
|
2021-05-15 04:49:41 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-02-28 21:17:12 +00:00
|
|
|
"""Set up the UpCloud server switch."""
|
2020-10-15 21:26:01 +00:00
|
|
|
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)
|
2018-02-28 21:17:12 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class UpCloudSwitch(UpCloudServerEntity, SwitchEntity):
|
2018-02-28 21:17:12 +00:00
|
|
|
"""Representation of an UpCloud server switch."""
|
|
|
|
|
2021-05-15 04:49:41 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2018-02-28 21:17:12 +00:00
|
|
|
"""Start the server."""
|
|
|
|
if self.state == STATE_OFF:
|
2020-10-15 21:26:01 +00:00
|
|
|
self._server.start()
|
2019-11-27 04:17:14 +00:00
|
|
|
dispatcher_send(self.hass, SIGNAL_UPDATE_UPCLOUD)
|
2018-02-28 21:17:12 +00:00
|
|
|
|
2021-05-15 04:49:41 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2018-02-28 21:17:12 +00:00
|
|
|
"""Stop the server."""
|
|
|
|
if self.is_on:
|
2020-10-15 21:26:01 +00:00
|
|
|
self._server.stop()
|