forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split googlehome to a component with device tracker platform (home-as…
…sistant#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
- Loading branch information
1 parent
e20c2aa
commit 632b204
Showing
5 changed files
with
161 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
Support Google Home units. | ||
For more details about this component, please refer to the documentation at | ||
https://home-assistant.io/components/googlehome/ | ||
""" | ||
import logging | ||
|
||
import asyncio | ||
import voluptuous as vol | ||
from homeassistant.const import CONF_DEVICES, CONF_HOST | ||
from homeassistant.helpers import discovery | ||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
REQUIREMENTS = ['googledevices==1.0.2'] | ||
|
||
DOMAIN = 'googlehome' | ||
CLIENT = 'googlehome_client' | ||
|
||
NAME = 'GoogleHome' | ||
|
||
CONF_DEVICE_TYPES = 'device_types' | ||
CONF_RSSI_THRESHOLD = 'rssi_threshold' | ||
|
||
DEVICE_TYPES = [1, 2, 3] | ||
DEFAULT_RSSI_THRESHOLD = -70 | ||
|
||
DEVICE_CONFIG = vol.Schema({ | ||
vol.Required(CONF_HOST): cv.string, | ||
vol.Optional(CONF_DEVICE_TYPES, | ||
default=DEVICE_TYPES): vol.All(cv.ensure_list, | ||
[vol.In(DEVICE_TYPES)]), | ||
vol.Optional(CONF_RSSI_THRESHOLD, | ||
default=DEFAULT_RSSI_THRESHOLD): vol.Coerce(int), | ||
}) | ||
|
||
|
||
CONFIG_SCHEMA = vol.Schema({ | ||
DOMAIN: vol.Schema({ | ||
vol.Required(CONF_DEVICES): vol.All(cv.ensure_list, [DEVICE_CONFIG]), | ||
}), | ||
}, extra=vol.ALLOW_EXTRA) | ||
|
||
|
||
async def async_setup(hass, config): | ||
"""Set up the Google Home component.""" | ||
hass.data[DOMAIN] = {} | ||
hass.data[CLIENT] = GoogleHomeClient(hass) | ||
|
||
for device in config[DOMAIN][CONF_DEVICES]: | ||
hass.data[DOMAIN][device['host']] = {} | ||
hass.async_create_task( | ||
discovery.async_load_platform( | ||
hass, 'device_tracker', DOMAIN, device, config)) | ||
|
||
return True | ||
|
||
|
||
class GoogleHomeClient: | ||
"""Handle all communication with the Google Home unit.""" | ||
|
||
def __init__(self, hass): | ||
"""Initialize the Google Home Client.""" | ||
self.hass = hass | ||
self._connected = None | ||
|
||
async def update_data(self, host): | ||
"""Update data from Google Home.""" | ||
from googledevices.api.connect import Cast | ||
_LOGGER.debug("Updating Google Home data for %s", host) | ||
session = async_get_clientsession(self.hass) | ||
|
||
device_info = await Cast(host, self.hass.loop, session).info() | ||
device_info_data = await device_info.get_device_info() | ||
self._connected = bool(device_info_data) | ||
|
||
bluetooth = await Cast(host, self.hass.loop, session).bluetooth() | ||
await bluetooth.scan_for_devices() | ||
await asyncio.sleep(5) | ||
bluetooth_data = await bluetooth.get_scan_result() | ||
|
||
self.hass.data[DOMAIN][host]['info'] = device_info_data | ||
self.hass.data[DOMAIN][host]['bluetooth'] = bluetooth_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters