core/homeassistant/components/upcloud/switch.py

39 lines
1.3 KiB
Python
Raw Normal View History

"""Support for interacting with UpCloud servers."""
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
from homeassistant.const import CONF_USERNAME, STATE_OFF
2021-05-15 04:49:41 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import dispatcher_send
2021-05-15 04:49:41 +00:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2021-12-16 14:14:46 +00:00
from . import DATA_UPCLOUD, SIGNAL_UPDATE_UPCLOUD, UpCloudServerEntity
2021-05-15 04:49:41 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the UpCloud server switch."""
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):
"""Representation of an UpCloud server switch."""
2021-05-15 04:49:41 +00:00
def turn_on(self, **kwargs: Any) -> None:
"""Start the server."""
if self.state == STATE_OFF:
self._server.start()
dispatcher_send(self.hass, SIGNAL_UPDATE_UPCLOUD)
2021-05-15 04:49:41 +00:00
def turn_off(self, **kwargs: Any) -> None:
"""Stop the server."""
if self.is_on:
self._server.stop()