Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for local_ip."""
2 
3 from homeassistant.components.network import async_get_source_ip
4 from homeassistant.components.sensor import SensorEntity
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_NAME
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from .const import SENSOR
11 
12 
14  hass: HomeAssistant,
15  entry: ConfigEntry,
16  async_add_entities: AddEntitiesCallback,
17 ) -> None:
18  """Set up the platform from config_entry."""
19  name = entry.data.get(CONF_NAME) or "Local IP"
20  async_add_entities([IPSensor(name)], True)
21 
22 
24  """A simple sensor."""
25 
26  _attr_unique_id = SENSOR
27  _attr_translation_key = "local_ip"
28 
29  def __init__(self, name: str) -> None:
30  """Initialize the sensor."""
31  self._attr_name_attr_name = name
32 
33  async def async_update(self) -> None:
34  """Fetch new state data for the sensor."""
35  self._attr_native_value_attr_native_value = await async_get_source_ip(self.hasshass)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:17
str async_get_source_ip(HomeAssistant hass, str|UndefinedType target_ip=UNDEFINED)
Definition: __init__.py:40