Add local_ip component (#29973)

* 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>
This commit is contained in:
Issac 2019-12-31 15:34:53 +02:00 committed by Martin Hjelmare
parent 5414e9d155
commit 3f570245aa
10 changed files with 179 additions and 0 deletions

View file

@ -0,0 +1,34 @@
"""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()