mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fixed NDMS for latest firmware (#15511)
* Fixed NDMS for latest firmware. Now using telnet instead of Web Interface * Using external library for NDMS interactions * updated requirements_all * renamed `mac` to `device` back * Using generators for name and attributes fetching
This commit is contained in:
parent
eeb79476de
commit
951372491c
@ -5,18 +5,18 @@ For more details about this platform, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/device_tracker.keenetic_ndms2/
|
https://home-assistant.io/components/device_tracker.keenetic_ndms2/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from collections import namedtuple
|
|
||||||
|
|
||||||
import requests
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.device_tracker import (
|
from homeassistant.components.device_tracker import (
|
||||||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
CONF_HOST, CONF_PORT, CONF_PASSWORD, CONF_USERNAME
|
||||||
)
|
)
|
||||||
|
|
||||||
|
REQUIREMENTS = ['ndms2_client==0.0.3']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Interface name to track devices for. Most likely one will not need to
|
# Interface name to track devices for. Most likely one will not need to
|
||||||
@ -25,11 +25,13 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
CONF_INTERFACE = 'interface'
|
CONF_INTERFACE = 'interface'
|
||||||
|
|
||||||
DEFAULT_INTERFACE = 'Home'
|
DEFAULT_INTERFACE = 'Home'
|
||||||
|
DEFAULT_PORT = 23
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
|
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
vol.Required(CONF_INTERFACE, default=DEFAULT_INTERFACE): cv.string,
|
vol.Required(CONF_INTERFACE, default=DEFAULT_INTERFACE): cv.string,
|
||||||
})
|
})
|
||||||
@ -42,21 +44,22 @@ def get_scanner(_hass, config):
|
|||||||
return scanner if scanner.success_init else None
|
return scanner if scanner.success_init else None
|
||||||
|
|
||||||
|
|
||||||
Device = namedtuple('Device', ['mac', 'name'])
|
|
||||||
|
|
||||||
|
|
||||||
class KeeneticNDMS2DeviceScanner(DeviceScanner):
|
class KeeneticNDMS2DeviceScanner(DeviceScanner):
|
||||||
"""This class scans for devices using keenetic NDMS2 web interface."""
|
"""This class scans for devices using keenetic NDMS2 web interface."""
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
"""Initialize the scanner."""
|
"""Initialize the scanner."""
|
||||||
|
from ndms2_client import Client, TelnetConnection
|
||||||
self.last_results = []
|
self.last_results = []
|
||||||
|
|
||||||
self._url = 'http://%s/rci/show/ip/arp' % config[CONF_HOST]
|
|
||||||
self._interface = config[CONF_INTERFACE]
|
self._interface = config[CONF_INTERFACE]
|
||||||
|
|
||||||
self._username = config.get(CONF_USERNAME)
|
self._client = Client(TelnetConnection(
|
||||||
self._password = config.get(CONF_PASSWORD)
|
config.get(CONF_HOST),
|
||||||
|
config.get(CONF_PORT),
|
||||||
|
config.get(CONF_USERNAME),
|
||||||
|
config.get(CONF_PASSWORD),
|
||||||
|
))
|
||||||
|
|
||||||
self.success_init = self._update_info()
|
self.success_init = self._update_info()
|
||||||
_LOGGER.info("Scanner initialized")
|
_LOGGER.info("Scanner initialized")
|
||||||
@ -69,53 +72,32 @@ class KeeneticNDMS2DeviceScanner(DeviceScanner):
|
|||||||
|
|
||||||
def get_device_name(self, device):
|
def get_device_name(self, device):
|
||||||
"""Return the name of the given device or None if we don't know."""
|
"""Return the name of the given device or None if we don't know."""
|
||||||
filter_named = [result.name for result in self.last_results
|
name = next((
|
||||||
if result.mac == device]
|
result.name for result in self.last_results
|
||||||
|
if result.mac == device), None)
|
||||||
|
return name
|
||||||
|
|
||||||
if filter_named:
|
def get_extra_attributes(self, device):
|
||||||
return filter_named[0]
|
"""Return the IP of the given device."""
|
||||||
return None
|
attributes = next((
|
||||||
|
{'ip': result.ip} for result in self.last_results
|
||||||
|
if result.mac == device), {})
|
||||||
|
return attributes
|
||||||
|
|
||||||
def _update_info(self):
|
def _update_info(self):
|
||||||
"""Get ARP from keenetic router."""
|
"""Get ARP from keenetic router."""
|
||||||
_LOGGER.info("Fetching...")
|
_LOGGER.debug("Fetching devices from router...")
|
||||||
|
|
||||||
last_results = []
|
from ndms2_client import ConnectionException
|
||||||
|
|
||||||
# doing a request
|
|
||||||
try:
|
try:
|
||||||
from requests.auth import HTTPDigestAuth
|
self.last_results = [
|
||||||
res = requests.get(self._url, timeout=10, auth=HTTPDigestAuth(
|
dev
|
||||||
self._username, self._password
|
for dev in self._client.get_devices()
|
||||||
))
|
if dev.interface == self._interface
|
||||||
except requests.exceptions.Timeout:
|
]
|
||||||
_LOGGER.error(
|
_LOGGER.debug("Successfully fetched data from router")
|
||||||
"Connection to the router timed out at URL %s", self._url)
|
return True
|
||||||
|
|
||||||
|
except ConnectionException:
|
||||||
|
_LOGGER.error("Error fetching data from router")
|
||||||
return False
|
return False
|
||||||
if res.status_code != 200:
|
|
||||||
_LOGGER.error(
|
|
||||||
"Connection failed with http code %s", res.status_code)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
result = res.json()
|
|
||||||
except ValueError:
|
|
||||||
# If json decoder could not parse the response
|
|
||||||
_LOGGER.error("Failed to parse response from router")
|
|
||||||
return False
|
|
||||||
|
|
||||||
# parsing response
|
|
||||||
for info in result:
|
|
||||||
if info.get('interface') != self._interface:
|
|
||||||
continue
|
|
||||||
mac = info.get('mac')
|
|
||||||
name = info.get('name')
|
|
||||||
# No address = no item :)
|
|
||||||
if mac is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
last_results.append(Device(mac.upper(), name))
|
|
||||||
|
|
||||||
self.last_results = last_results
|
|
||||||
|
|
||||||
_LOGGER.info("Request successful")
|
|
||||||
return True
|
|
||||||
|
@ -579,6 +579,9 @@ nad_receiver==0.0.9
|
|||||||
# homeassistant.components.light.nanoleaf_aurora
|
# homeassistant.components.light.nanoleaf_aurora
|
||||||
nanoleaf==0.4.1
|
nanoleaf==0.4.1
|
||||||
|
|
||||||
|
# homeassistant.components.device_tracker.keenetic_ndms2
|
||||||
|
ndms2_client==0.0.3
|
||||||
|
|
||||||
# homeassistant.components.sensor.netdata
|
# homeassistant.components.sensor.netdata
|
||||||
netdata==0.1.2
|
netdata==0.1.2
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user