Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for interacting with UpCloud servers."""
2 
3 from typing import Any
4 
5 from homeassistant.components.switch import SwitchEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_USERNAME, STATE_OFF
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.dispatcher import dispatcher_send
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DATA_UPCLOUD
13 from .entity import UpCloudServerEntity
14 
15 SIGNAL_UPDATE_UPCLOUD = "upcloud_update"
16 
17 
19  hass: HomeAssistant,
20  config_entry: ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up the UpCloud server switch."""
24  coordinator = hass.data[DATA_UPCLOUD].coordinators[config_entry.data[CONF_USERNAME]]
25  entities = [UpCloudSwitch(coordinator, uuid) for uuid in coordinator.data]
26  async_add_entities(entities, True)
27 
28 
30  """Representation of an UpCloud server switch."""
31 
32  def turn_on(self, **kwargs: Any) -> None:
33  """Start the server."""
34  if self.statestatestatestate == STATE_OFF:
35  self._server_server.start()
36  dispatcher_send(self.hasshasshass, SIGNAL_UPDATE_UPCLOUD)
37 
38  def turn_off(self, **kwargs: Any) -> None:
39  """Stop the server."""
40  if self.is_onis_onis_on:
41  self._server_server.stop()
Literal["on", "off"]|None state(self)
Definition: entity.py:1686
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22
None dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:137