Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Pilight sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
11  SensorEntity,
12 )
13 from homeassistant.const import CONF_NAME, CONF_PAYLOAD, CONF_UNIT_OF_MEASUREMENT
14 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
18 
19 from . import EVENT
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 CONF_VARIABLE = "variable"
24 
25 DEFAULT_NAME = "Pilight Sensor"
26 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
27  {
28  vol.Required(CONF_VARIABLE): cv.string,
29  vol.Required(CONF_PAYLOAD): vol.Schema(dict),
30  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
31  vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
32  }
33 )
34 
35 
37  hass: HomeAssistant,
38  config: ConfigType,
39  add_entities: AddEntitiesCallback,
40  discovery_info: DiscoveryInfoType | None = None,
41 ) -> None:
42  """Set up Pilight Sensor."""
44  [
46  hass=hass,
47  name=config.get(CONF_NAME),
48  variable=config.get(CONF_VARIABLE),
49  payload=config.get(CONF_PAYLOAD),
50  unit_of_measurement=config.get(CONF_UNIT_OF_MEASUREMENT),
51  )
52  ]
53  )
54 
55 
57  """Representation of a sensor that can be updated using Pilight."""
58 
59  _attr_should_poll = False
60 
61  def __init__(self, hass, name, variable, payload, unit_of_measurement):
62  """Initialize the sensor."""
63  self._state_state = None
64  self._hass_hass = hass
65  self._name_name = name
66  self._variable_variable = variable
67  self._payload_payload = payload
68  self._unit_of_measurement_unit_of_measurement = unit_of_measurement
69 
70  hass.bus.listen(EVENT, self._handle_code_handle_code)
71 
72  @property
73  def name(self):
74  """Return the name of the sensor."""
75  return self._name_name
76 
77  @property
79  """Return the unit this state is expressed in."""
80  return self._unit_of_measurement_unit_of_measurement
81 
82  @property
83  def native_value(self):
84  """Return the state of the entity."""
85  return self._state_state
86 
87  def _handle_code(self, call):
88  """Handle received code by the pilight-daemon.
89 
90  If the code matches the defined payload
91  of this sensor the sensor state is changed accordingly.
92  """
93  # Check if received code matches defined payload
94  # True if payload is contained in received code dict, not
95  # all items have to match
96  if self._payload_payload.items() <= call.data.items():
97  try:
98  value = call.data[self._variable_variable]
99  self._state_state = value
100  self.schedule_update_ha_stateschedule_update_ha_state()
101  except KeyError:
102  _LOGGER.error(
103  "No variable %s in received code data %s",
104  str(self._variable_variable),
105  str(call.data),
106  )
def __init__(self, hass, name, variable, payload, unit_of_measurement)
Definition: sensor.py:61
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:41