Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Iperf3 sensors."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
6 from homeassistant.const import CONF_MONITORED_CONDITIONS
7 from homeassistant.core import HomeAssistant, callback
8 from homeassistant.helpers.dispatcher import async_dispatcher_connect
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 from homeassistant.helpers.restore_state import RestoreEntity
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import ATTR_VERSION, DATA_UPDATED, DOMAIN as IPERF3_DOMAIN, SENSOR_TYPES
14 
15 ATTR_PROTOCOL = "Protocol"
16 ATTR_REMOTE_HOST = "Remote Server"
17 ATTR_REMOTE_PORT = "Remote Port"
18 
19 
21  hass: HomeAssistant,
22  config: ConfigType,
23  async_add_entities: AddEntitiesCallback,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> None:
26  """Set up the Iperf3 sensor."""
27  if not discovery_info:
28  return
29 
30  entities = [
31  Iperf3Sensor(iperf3_host, description)
32  for iperf3_host in hass.data[IPERF3_DOMAIN].values()
33  for description in SENSOR_TYPES
34  if description.key in discovery_info[CONF_MONITORED_CONDITIONS]
35  ]
36  async_add_entities(entities, True)
37 
38 
39 # pylint: disable-next=hass-invalid-inheritance # needs fixing
41  """A Iperf3 sensor implementation."""
42 
43  _attr_attribution = "Data retrieved using Iperf3"
44  _attr_should_poll = False
45 
46  def __init__(self, iperf3_data, description: SensorEntityDescription) -> None:
47  """Initialize the sensor."""
48  self.entity_descriptionentity_description = description
49  self._iperf3_data_iperf3_data = iperf3_data
50  self._attr_name_attr_name = f"{description.name} {iperf3_data.host}"
51 
52  @property
54  """Return the state attributes."""
55  return {
56  ATTR_PROTOCOL: self._iperf3_data_iperf3_data.protocol,
57  ATTR_REMOTE_HOST: self._iperf3_data_iperf3_data.host,
58  ATTR_REMOTE_PORT: self._iperf3_data_iperf3_data.port,
59  ATTR_VERSION: self._iperf3_data_iperf3_data.data[ATTR_VERSION],
60  }
61 
62  async def async_added_to_hass(self) -> None:
63  """Handle entity which will be added."""
64  await super().async_added_to_hass()
65 
66  self.async_on_removeasync_on_remove(
68  self.hasshass, DATA_UPDATED, self._schedule_immediate_update_schedule_immediate_update
69  )
70  )
71 
72  if not (state := await self.async_get_last_stateasync_get_last_state()):
73  return
74  self._attr_native_value_attr_native_value = state.state
75 
76  def update(self) -> None:
77  """Get the latest data and update the states."""
78  data = self._iperf3_data_iperf3_data.data.get(self.entity_descriptionentity_description.key)
79  if data is not None:
80  self._attr_native_value_attr_native_value = round(data, 2)
81 
82  @callback
83  def _schedule_immediate_update(self, host):
84  if host == self._iperf3_data_iperf3_data.host:
85  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
None __init__(self, iperf3_data, SensorEntityDescription description)
Definition: sensor.py:46
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:25
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103