Split googlehome to a component with device tracker platform (#19971)
* Add component for googlehome
* Add missing name in CODEOWNERS
* Linting issues
* googledevices version bump
* Use NAME from component instead of DOMAIN
* Cleaner handling of accepted devices in for loop
* Fixes one linting issue
* Validate device_types
* Fixes one linting issue
* Fixes linting issue
* Revert 0abb642
and import DOMAIN as GOOGLEHOME_DOMAIN
* Return false if discovery_info is None
* Combine if's in for loop
* Use async_load_platfrom
* Fix line length
* Add error message to user
* Shorter log message
* error -> warning, remove period
* Update .coveragerc
* Move to correct place
This commit is contained in:
parent
e20c2aa113
commit
632b2042e4
5 changed files with 159 additions and 77 deletions
|
@ -5,91 +5,82 @@ For more details about this platform, please refer to the documentation at
|
|||
https://home-assistant.io/components/device_tracker.googlehome/
|
||||
"""
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
from homeassistant.components.device_tracker import DeviceScanner
|
||||
from homeassistant.components.googlehome import (
|
||||
CLIENT, DOMAIN as GOOGLEHOME_DOMAIN, NAME)
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
from homeassistant.util import slugify
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.components.device_tracker import (
|
||||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
||||
from homeassistant.const import CONF_HOST
|
||||
DEPENDENCIES = ['googlehome']
|
||||
|
||||
REQUIREMENTS = ['ghlocalapi==0.3.5']
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=10)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_RSSI_THRESHOLD = 'rssi_threshold'
|
||||
|
||||
PLATFORM_SCHEMA = vol.All(
|
||||
PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_RSSI_THRESHOLD, default=-70): vol.Coerce(int),
|
||||
}))
|
||||
|
||||
|
||||
async def async_get_scanner(hass, config):
|
||||
"""Validate the configuration and return an Google Home scanner."""
|
||||
scanner = GoogleHomeDeviceScanner(hass, config[DOMAIN])
|
||||
await scanner.async_connect()
|
||||
return scanner if scanner.success_init else None
|
||||
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
|
||||
"""Validate the configuration and return a Google Home scanner."""
|
||||
if discovery_info is None:
|
||||
_LOGGER.warning(
|
||||
"To use this you need to configure the 'googlehome' component")
|
||||
return False
|
||||
scanner = GoogleHomeDeviceScanner(hass, hass.data[CLIENT],
|
||||
discovery_info, async_see)
|
||||
return await scanner.async_init()
|
||||
|
||||
|
||||
class GoogleHomeDeviceScanner(DeviceScanner):
|
||||
"""This class queries a Google Home unit."""
|
||||
|
||||
def __init__(self, hass, config):
|
||||
def __init__(self, hass, client, config, async_see):
|
||||
"""Initialize the scanner."""
|
||||
from ghlocalapi.device_info import DeviceInfo
|
||||
from ghlocalapi.bluetooth import Bluetooth
|
||||
self.async_see = async_see
|
||||
self.hass = hass
|
||||
self.rssi = config['rssi_threshold']
|
||||
self.device_types = config['device_types']
|
||||
self.host = config['host']
|
||||
self.client = client
|
||||
|
||||
self.last_results = {}
|
||||
async def async_init(self):
|
||||
"""Further initialize connection to Google Home."""
|
||||
await self.client.update_data(self.host)
|
||||
data = self.hass.data[GOOGLEHOME_DOMAIN][self.host]
|
||||
info = data.get('info', {})
|
||||
connected = bool(info)
|
||||
if connected:
|
||||
await self.async_update()
|
||||
async_track_time_interval(self.hass,
|
||||
self.async_update,
|
||||
DEFAULT_SCAN_INTERVAL)
|
||||
return connected
|
||||
|
||||
self.success_init = False
|
||||
self._host = config[CONF_HOST]
|
||||
self.rssi_threshold = config[CONF_RSSI_THRESHOLD]
|
||||
|
||||
session = async_get_clientsession(hass)
|
||||
self.deviceinfo = DeviceInfo(hass.loop, session, self._host)
|
||||
self.scanner = Bluetooth(hass.loop, session, self._host)
|
||||
|
||||
async def async_connect(self):
|
||||
"""Initialize connection to Google Home."""
|
||||
await self.deviceinfo.get_device_info()
|
||||
data = self.deviceinfo.device_info
|
||||
self.success_init = data is not None
|
||||
|
||||
async def async_scan_devices(self):
|
||||
"""Scan for new devices and return a list with found device IDs."""
|
||||
await self.async_update_info()
|
||||
return list(self.last_results.keys())
|
||||
|
||||
async def async_get_device_name(self, device):
|
||||
"""Return the name of the given device or None if we don't know."""
|
||||
if device not in self.last_results:
|
||||
return None
|
||||
return '{}_{}'.format(self._host,
|
||||
self.last_results[device]['btle_mac_address'])
|
||||
|
||||
async def get_extra_attributes(self, device):
|
||||
"""Return the extra attributes of the device."""
|
||||
return self.last_results[device]
|
||||
|
||||
async def async_update_info(self):
|
||||
async def async_update(self, now=None):
|
||||
"""Ensure the information from Google Home is up to date."""
|
||||
_LOGGER.debug('Checking Devices...')
|
||||
await self.scanner.scan_for_devices()
|
||||
await self.scanner.get_scan_result()
|
||||
ghname = self.deviceinfo.device_info['name']
|
||||
devices = {}
|
||||
for device in self.scanner.devices:
|
||||
if device['rssi'] > self.rssi_threshold:
|
||||
uuid = '{}_{}'.format(self._host, device['mac_address'])
|
||||
devices[uuid] = {}
|
||||
devices[uuid]['rssi'] = device['rssi']
|
||||
devices[uuid]['btle_mac_address'] = device['mac_address']
|
||||
devices[uuid]['ghname'] = ghname
|
||||
devices[uuid]['source_type'] = 'bluetooth'
|
||||
if device['name']:
|
||||
devices[uuid]['btle_name'] = device['name']
|
||||
await self.scanner.clear_scan_result()
|
||||
self.last_results = devices
|
||||
_LOGGER.debug('Checking Devices on %s', self.host)
|
||||
await self.client.update_data(self.host)
|
||||
data = self.hass.data[GOOGLEHOME_DOMAIN][self.host]
|
||||
info = data.get('info')
|
||||
bluetooth = data.get('bluetooth')
|
||||
if info is None or bluetooth is None:
|
||||
return
|
||||
google_home_name = info.get('name', NAME)
|
||||
|
||||
for device in bluetooth:
|
||||
if (device['device_type'] not in
|
||||
self.device_types or device['rssi'] < self.rssi):
|
||||
continue
|
||||
|
||||
name = "{} {}".format(self.host, device['mac_address'])
|
||||
|
||||
attributes = {}
|
||||
attributes['btle_mac_address'] = device['mac_address']
|
||||
attributes['ghname'] = google_home_name
|
||||
attributes['rssi'] = device['rssi']
|
||||
attributes['source_type'] = 'bluetooth'
|
||||
if device['name']:
|
||||
attributes['name'] = device['name']
|
||||
|
||||
await self.async_see(dev_id=slugify(name),
|
||||
attributes=attributes)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue