2019-12-31 13:34:53 +00:00
|
|
|
"""Sensor platform for local_ip."""
|
|
|
|
|
2021-07-22 18:12:33 +00:00
|
|
|
from homeassistant.components.network import async_get_source_ip
|
2021-03-22 18:59:03 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-05-27 12:04:40 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-04-09 14:06:01 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-05-27 12:04:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-31 13:34:53 +00:00
|
|
|
|
2020-04-09 14:06:01 +00:00
|
|
|
from .const import DOMAIN, SENSOR
|
2019-12-31 13:34:53 +00:00
|
|
|
|
2020-04-09 14:06:01 +00:00
|
|
|
|
2021-05-27 12:04:40 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-12-31 13:34:53 +00:00
|
|
|
"""Set up the platform from config_entry."""
|
2021-05-27 12:04:40 +00:00
|
|
|
name = entry.data.get(CONF_NAME) or DOMAIN
|
2019-12-31 13:34:53 +00:00
|
|
|
async_add_entities([IPSensor(name)], True)
|
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class IPSensor(SensorEntity):
|
2019-12-31 13:34:53 +00:00
|
|
|
"""A simple sensor."""
|
|
|
|
|
2021-05-27 12:04:40 +00:00
|
|
|
_attr_unique_id = SENSOR
|
|
|
|
_attr_icon = "mdi:ip"
|
|
|
|
|
|
|
|
def __init__(self, name: str) -> None:
|
2019-12-31 13:34:53 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-05-27 12:04:40 +00:00
|
|
|
self._attr_name = name
|
|
|
|
|
2021-07-22 18:12:33 +00:00
|
|
|
async def async_update(self) -> None:
|
2019-12-31 13:34:53 +00:00
|
|
|
"""Fetch new state data for the sensor."""
|
2021-11-15 17:18:57 +00:00
|
|
|
self._attr_native_value = await async_get_source_ip(self.hass)
|