mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Inconsistent entity_id when multiple sensors (#16205)
* Inconsistent entity_id when multiple sensors I am submitting a change to fix a [bug](https://github.com/home-assistant/home-assistant/issues/16204) for when there are several sensors for the same hostname. For example I want to track my IPv4 and IPv6 address. It creates two entities that regularly switch ids based on the order they get initialized. To fix this I comform to the way other componnents have addressed the issue by adding an optional `name` attribute. * Line too long * Removing trailing whitespace
This commit is contained in:
parent
5341785aae
commit
3032de1dc1
@ -19,11 +19,13 @@ REQUIREMENTS = ['aiodns==1.1.1']
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
CONF_NAME = 'name'
|
||||||
CONF_HOSTNAME = 'hostname'
|
CONF_HOSTNAME = 'hostname'
|
||||||
CONF_RESOLVER = 'resolver'
|
CONF_RESOLVER = 'resolver'
|
||||||
CONF_RESOLVER_IPV6 = 'resolver_ipv6'
|
CONF_RESOLVER_IPV6 = 'resolver_ipv6'
|
||||||
CONF_IPV6 = 'ipv6'
|
CONF_IPV6 = 'ipv6'
|
||||||
|
|
||||||
|
DEFAULT_NAME = 'myip'
|
||||||
DEFAULT_HOSTNAME = 'myip.opendns.com'
|
DEFAULT_HOSTNAME = 'myip.opendns.com'
|
||||||
DEFAULT_RESOLVER = '208.67.222.222'
|
DEFAULT_RESOLVER = '208.67.222.222'
|
||||||
DEFAULT_RESOLVER_IPV6 = '2620:0:ccc::2'
|
DEFAULT_RESOLVER_IPV6 = '2620:0:ccc::2'
|
||||||
@ -32,6 +34,7 @@ DEFAULT_IPV6 = False
|
|||||||
SCAN_INTERVAL = timedelta(seconds=120)
|
SCAN_INTERVAL = timedelta(seconds=120)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Optional(CONF_NAME): cv.string,
|
||||||
vol.Optional(CONF_HOSTNAME, default=DEFAULT_HOSTNAME): cv.string,
|
vol.Optional(CONF_HOSTNAME, default=DEFAULT_HOSTNAME): cv.string,
|
||||||
vol.Optional(CONF_RESOLVER, default=DEFAULT_RESOLVER): cv.string,
|
vol.Optional(CONF_RESOLVER, default=DEFAULT_RESOLVER): cv.string,
|
||||||
vol.Optional(CONF_RESOLVER_IPV6, default=DEFAULT_RESOLVER_IPV6): cv.string,
|
vol.Optional(CONF_RESOLVER_IPV6, default=DEFAULT_RESOLVER_IPV6): cv.string,
|
||||||
@ -40,28 +43,34 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup_platform(hass, config, async_add_entities,
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||||
discovery_info=None):
|
|
||||||
"""Set up the DNS IP sensor."""
|
"""Set up the DNS IP sensor."""
|
||||||
hostname = config.get(CONF_HOSTNAME)
|
hostname = config.get(CONF_HOSTNAME)
|
||||||
|
name = config.get(CONF_NAME)
|
||||||
|
if not name:
|
||||||
|
if hostname == DEFAULT_HOSTNAME:
|
||||||
|
name = DEFAULT_NAME
|
||||||
|
else:
|
||||||
|
name = hostname
|
||||||
ipv6 = config.get(CONF_IPV6)
|
ipv6 = config.get(CONF_IPV6)
|
||||||
if ipv6:
|
if ipv6:
|
||||||
resolver = config.get(CONF_RESOLVER_IPV6)
|
resolver = config.get(CONF_RESOLVER_IPV6)
|
||||||
else:
|
else:
|
||||||
resolver = config.get(CONF_RESOLVER)
|
resolver = config.get(CONF_RESOLVER)
|
||||||
|
|
||||||
async_add_entities([WanIpSensor(
|
async_add_devices([WanIpSensor(
|
||||||
hass, hostname, resolver, ipv6)], True)
|
hass, name, hostname, resolver, ipv6)], True)
|
||||||
|
|
||||||
|
|
||||||
class WanIpSensor(Entity):
|
class WanIpSensor(Entity):
|
||||||
"""Implementation of a DNS IP sensor."""
|
"""Implementation of a DNS IP sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, hostname, resolver, ipv6):
|
def __init__(self, hass, name, hostname, resolver, ipv6):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
import aiodns
|
import aiodns
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = hostname
|
self._name = name
|
||||||
|
self.hostname = hostname
|
||||||
self.resolver = aiodns.DNSResolver(loop=self.hass.loop)
|
self.resolver = aiodns.DNSResolver(loop=self.hass.loop)
|
||||||
self.resolver.nameservers = [resolver]
|
self.resolver.nameservers = [resolver]
|
||||||
self.querytype = 'AAAA' if ipv6 else 'A'
|
self.querytype = 'AAAA' if ipv6 else 'A'
|
||||||
@ -80,7 +89,8 @@ class WanIpSensor(Entity):
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_update(self):
|
def async_update(self):
|
||||||
"""Get the current DNS IP address for hostname."""
|
"""Get the current DNS IP address for hostname."""
|
||||||
response = yield from self.resolver.query(self._name, self.querytype)
|
response = yield from self.resolver.query(self.hostname,
|
||||||
|
self.querytype)
|
||||||
if response:
|
if response:
|
||||||
self._state = response[0].host
|
self._state = response[0].host
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user