Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added device tracker support for Ubee Router #19586

Merged
merged 9 commits into from Feb 21, 2019
Copy path View file
@@ -536,6 +536,7 @@ omit =
homeassistant/components/device_tracker/tplink.py
homeassistant/components/device_tracker/traccar.py
homeassistant/components/device_tracker/trackr.py
homeassistant/components/device_tracker/ubee.py
homeassistant/components/device_tracker/ubus.py
homeassistant/components/downloader.py
homeassistant/components/emoncms_history.py
@@ -0,0 +1,97 @@
"""
Support for Ubee router.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.ubee/
"""

import logging
import voluptuous as vol

from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME)
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['pyubee==0.2']

_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
})


def get_scanner(hass, config):
"""Validate the configuration and return a Ubee scanner."""
try:
return UbeeDeviceScanner(config[DOMAIN])
except ConnectionError:
return None


class UbeeDeviceScanner(DeviceScanner):
"""This class queries a wireless Ubee router."""

def __init__(self, config):
"""Initialize the Ubee scanner."""
from pyubee import Ubee

self.host = config[CONF_HOST]
self.username = config[CONF_USERNAME]
self.password = config[CONF_PASSWORD]

self.last_results = {}
self.mac2name = {}

self.ubee = Ubee(self.host, self.username, self.password)
_LOGGER.info("Logging in")

This comment has been minimized.

Copy link
@MartinHjelmare

MartinHjelmare Feb 21, 2019

Member

We don't want logging in init method.

results = self.get_connected_devices()
self.success_init = results is not None

if self.success_init:
self.last_results = results
else:
_LOGGER.error("Login failed")

This comment has been minimized.

Copy link
@MartinHjelmare

MartinHjelmare Feb 21, 2019

Member

We should fail the platform setup here by returning None in get_scanner.


def scan_devices(self):
This conversation was marked as resolved by mzdrale

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Dec 28, 2018

too many blank lines (2)

This comment has been minimized.

Copy link
@mzdrale

mzdrale Jan 10, 2019

Author Contributor

Fixed.

"""Scan for new devices and return a list with found device IDs."""
self._update_info()

return self.last_results

def get_device_name(self, device):
"""Return the name of the given device or None if we don't know."""
if device in self.mac2name:
return self.mac2name.get(device)

return None

def _update_info(self):
"""Retrieve latest information from the Ubee router.
Returns boolean if scanning successful.
"""
if not self.success_init:
return False

This comment has been minimized.

Copy link
@ludeeus

ludeeus Jan 19, 2019

Contributor

just return, the value is not used.

This comment has been minimized.

Copy link
@mzdrale

mzdrale Jan 22, 2019

Author Contributor

Fixed.


_LOGGER.info("Scanning")

This comment has been minimized.

Copy link
@ludeeus

ludeeus Jan 19, 2019

Contributor

This will log an info message every 12sec.
debug will probably suffice here.

This comment has been minimized.

Copy link
@mzdrale

mzdrale Jan 22, 2019

Author Contributor

I wrote this component based on code for ddwrt, ubus and some other device tracker components, which are part of Home Assistant for a long time. They all log an info every few seconds, but I agree with you, that's too much.

results = self.get_connected_devices()

if results is None:
_LOGGER.warning("Error scanning devices")
return False

self.last_results = results or []

return True

This comment has been minimized.

Copy link
@ludeeus

ludeeus Jan 19, 2019

Contributor

This is not used.

This comment has been minimized.

Copy link
@mzdrale

mzdrale Jan 22, 2019

Author Contributor

Fixed.


def get_connected_devices(self):
"""List connected devices with pyubee."""
if not self.ubee.session_active():
self.ubee.login()

return self.ubee.get_connected_devices()
Copy path View file
@@ -1344,6 +1344,9 @@ pytradfri[async]==6.0.1
# homeassistant.components.sensor.trafikverket_weatherstation
pytrafikverket==0.1.5.8

# homeassistant.components.device_tracker.ubee
pyubee==0.2

# homeassistant.components.device_tracker.unifi
pyunifi==2.13

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.