* Added localip component * Split config and core logic, and migrate to sensor platform (requested by @MartinHjelmare) Also allow overriding the sensor name via the config * Tweak docstring Co-Authored-By: Fabian Affolter <mail@fabian-affolter.ch> * Initial support for config entries * Rename localip to local_ip (1/2) * Rename localip to local_ip (2/2) * Add test for config_flow * Split and rename tests * Remove unneeded code from config_flow * Implement configuration as config entry import. Other misc requested changes from code review. * Fix tests * minor code review fixes * remove unneeded code Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
34 lines
898 B
Python
34 lines
898 B
Python
"""Sensor platform for local_ip."""
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity import Entity
|
|
from homeassistant.util import get_local_ip
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entities):
|
|
"""Set up the platform from config_entry."""
|
|
name = config_entry.data["name"]
|
|
async_add_entities([IPSensor(name)], True)
|
|
|
|
|
|
class IPSensor(Entity):
|
|
"""A simple sensor."""
|
|
|
|
def __init__(self, name: str):
|
|
"""Initialize the sensor."""
|
|
self._state = None
|
|
self._name = name
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the sensor."""
|
|
return self._name
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the state of the sensor."""
|
|
return self._state
|
|
|
|
def update(self):
|
|
"""Fetch new state data for the sensor."""
|
|
self._state = get_local_ip()
|