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

Add support for Google Home device tracking #18190

Merged
merged 5 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ omit =
homeassistant/components/device_tracker/freebox.py
homeassistant/components/device_tracker/fritz.py
homeassistant/components/device_tracker/google_maps.py
homeassistant/components/device_tracker/googlehome.py
homeassistant/components/device_tracker/gpslogger.py
homeassistant/components/device_tracker/hitron_coda.py
homeassistant/components/device_tracker/huawei_router.py
Expand Down
92 changes: 92 additions & 0 deletions homeassistant/components/device_tracker/googlehome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Support for Google Home bluetooth tacker.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.googlehome/
"""
import logging

import voluptuous as vol

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

REQUIREMENTS = ['ghlocalapi==0.0.1']

_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


class GoogleHomeDeviceScanner(DeviceScanner):
"""This class queries a Google Home unit."""

def __init__(self, hass, config):
"""Initialize the scanner."""
from ghlocalapi.device_info import DeviceInfo
from ghlocalapi.bluetooth import BluetoothScan

self.last_results = {}

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 = BluetoothScan(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):
"""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'
self.last_results = devices
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ geojson_client==0.3
# homeassistant.components.sensor.geo_rss_events
georss_client==0.4

# homeassistant.components.device_tracker.googlehome
ghlocalapi==0.0.1

# homeassistant.components.sensor.gitter
gitterpy==0.1.7

Expand Down