Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for X10 lights."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from subprocess import STDOUT, CalledProcessError, check_output
7 from typing import Any
8 
9 import voluptuous as vol
10 
12  ATTR_BRIGHTNESS,
13  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
14  ColorMode,
15  LightEntity,
16 )
17 from homeassistant.const import CONF_DEVICES, CONF_ID, CONF_NAME
18 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
26  {
27  vol.Required(CONF_DEVICES): vol.All(
28  cv.ensure_list,
29  [{vol.Required(CONF_ID): cv.string, vol.Required(CONF_NAME): cv.string}],
30  )
31  }
32 )
33 
34 
35 def x10_command(command):
36  """Execute X10 command and check output."""
37  return check_output(["heyu", *command.split(" ")], stderr=STDOUT)
38 
39 
40 def get_unit_status(code):
41  """Get on/off status for given unit."""
42  output = check_output(["heyu", "onstate", code])
43  return int(output.decode("utf-8")[0])
44 
45 
47  hass: HomeAssistant,
48  config: ConfigType,
49  add_entities: AddEntitiesCallback,
50  discovery_info: DiscoveryInfoType | None = None,
51 ) -> None:
52  """Set up the x10 Light platform."""
53  is_cm11a = True
54  try:
55  x10_command("info")
56  except CalledProcessError as err:
57  _LOGGER.warning("Assuming that the device is CM17A: %s", err.output)
58  is_cm11a = False
59 
60  add_entities(X10Light(light, is_cm11a) for light in config[CONF_DEVICES])
61 
62 
64  """Representation of an X10 Light."""
65 
66  _attr_color_mode = ColorMode.BRIGHTNESS
67  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
68 
69  def __init__(self, light, is_cm11a):
70  """Initialize an X10 Light."""
71  self._name_name = light["name"]
72  self._id_id = light["id"]
73  self._brightness_brightness = 0
74  self._state_state = False
75  self._is_cm11a_is_cm11a = is_cm11a
76 
77  @property
78  def name(self):
79  """Return the display name of this light."""
80  return self._name_name
81 
82  @property
83  def brightness(self):
84  """Return the brightness of the light."""
85  return self._brightness_brightness
86 
87  @property
88  def is_on(self):
89  """Return true if light is on."""
90  return self._state_state
91 
92  def turn_on(self, **kwargs: Any) -> None:
93  """Instruct the light to turn on."""
94  if self._is_cm11a_is_cm11a:
95  x10_command(f"on {self._id}")
96  else:
97  x10_command(f"fon {self._id}")
98  self._brightness_brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
99  self._state_state = True
100 
101  def turn_off(self, **kwargs: Any) -> None:
102  """Instruct the light to turn off."""
103  if self._is_cm11a_is_cm11a:
104  x10_command(f"off {self._id}")
105  else:
106  x10_command(f"foff {self._id}")
107  self._state_state = False
108 
109  def update(self) -> None:
110  """Fetch update state."""
111  if self._is_cm11a_is_cm11a:
112  self._state_state = bool(get_unit_status(self._id_id))
113  else:
114  # Not supported on CM17A
115  pass
None turn_on(self, **Any kwargs)
Definition: light.py:92
def __init__(self, light, is_cm11a)
Definition: light.py:69
None turn_off(self, **Any kwargs)
Definition: light.py:101
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:51