Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch platform for MicroBot."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import config_validation as cv
14  AddEntitiesCallback,
15  async_get_current_platform,
16 )
17 from homeassistant.helpers.typing import VolDictType
18 
19 from .const import DOMAIN
20 from .coordinator import MicroBotDataUpdateCoordinator
21 from .entity import MicroBotEntity
22 
23 CALIBRATE = "calibrate"
24 CALIBRATE_SCHEMA: VolDictType = {
25  vol.Required("depth"): cv.positive_int,
26  vol.Required("duration"): cv.positive_int,
27  vol.Required("mode"): vol.In(["normal", "invert", "toggle"]),
28 }
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Set up MicroBot based on a config entry."""
35  coordinator: MicroBotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
36  async_add_entities([MicroBotBinarySwitch(coordinator, entry)])
37  platform = async_get_current_platform()
38  platform.async_register_entity_service(
39  CALIBRATE,
40  CALIBRATE_SCHEMA,
41  "async_calibrate",
42  )
43 
44 
46  """MicroBot switch class."""
47 
48  _attr_translation_key = "push"
49 
50  async def async_turn_on(self, **kwargs: Any) -> None:
51  """Turn on the switch."""
52  await self.coordinator.api.push_on()
53  self.async_write_ha_stateasync_write_ha_state()
54 
55  async def async_turn_off(self, **kwargs: Any) -> None:
56  """Turn off the switch."""
57  await self.coordinator.api.push_off()
58  self.async_write_ha_stateasync_write_ha_state()
59 
60  @property
61  def is_on(self) -> bool:
62  """Return true if the switch is on."""
63  return self.coordinator.api.is_on
64 
65  async def async_calibrate(
66  self,
67  depth: int,
68  duration: int,
69  mode: str,
70  ) -> None:
71  """Send calibration commands to the switch."""
72  await self.coordinator.api.calibrate(depth, duration, mode)
None async_calibrate(self, int depth, int duration, str mode)
Definition: switch.py:70
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:33