Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Switchmate."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 from switchmate import Switchmate
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
13  SwitchEntity,
14 )
15 from homeassistant.const import CONF_MAC, CONF_NAME
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 CONF_FLIP_ON_OFF = "flip_on_off"
22 DEFAULT_NAME = "Switchmate"
23 
24 SCAN_INTERVAL = timedelta(minutes=30)
25 
26 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
27  {
28  vol.Required(CONF_MAC): cv.string,
29  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
30  vol.Optional(CONF_FLIP_ON_OFF, default=False): cv.boolean,
31  }
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  add_entities: AddEntitiesCallback,
39  discovery_info: DiscoveryInfoType | None = None,
40 ) -> None:
41  """Perform the setup for Switchmate devices."""
42  name = config.get(CONF_NAME)
43  mac_addr = config[CONF_MAC]
44  flip_on_off = config[CONF_FLIP_ON_OFF]
45  add_entities([SwitchmateEntity(mac_addr, name, flip_on_off)], True)
46 
47 
49  """Representation of a Switchmate."""
50 
51  def __init__(self, mac, name, flip_on_off) -> None:
52  """Initialize the Switchmate."""
53 
54  self._mac_mac = mac
55  self._name_name = name
56  self._device_device = Switchmate(mac=mac, flip_on_off=flip_on_off)
57 
58  @property
59  def unique_id(self) -> str:
60  """Return a unique, Home Assistant friendly identifier for this entity."""
61  return self._mac_mac.replace(":", "")
62 
63  @property
64  def available(self) -> bool:
65  """Return True if entity is available."""
66  return self._device_device.available
67 
68  @property
69  def name(self) -> str:
70  """Return the name of the switch."""
71  return self._name_name
72 
73  async def async_update(self) -> None:
74  """Synchronize state with switch."""
75  await self._device_device.update()
76 
77  @property
78  def is_on(self) -> bool:
79  """Return true if it is on."""
80  return self._device_device.state
81 
82  async def async_turn_on(self, **kwargs: Any) -> None:
83  """Turn the switch on."""
84  await self._device_device.turn_on()
85 
86  async def async_turn_off(self, **kwargs: Any) -> None:
87  """Turn the switch off."""
88  await self._device_device.turn_off()
None __init__(self, mac, name, flip_on_off)
Definition: switch.py:51
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:40