async def reboot_device(host, loop): """Reboot a Google Home unit.""" from googledevices.api.cast.settings import Settings async with gdh_session() as session: googledevices = Settings(host, loop, session) await googledevices.reboot()
async def get_host(self): """Get the hostname/IP of the WiFi unit.""" if self.host is not None: self._wifi_host = self.host else: import async_timeout for host in WIFIHOSTS: try: if self.loop is None: self.loop = gdh_loop() if self.session is None: self.session = gdh_session() url = API.format(schema="http", host=host, port="", endpoint=self.endpoint) async with async_timeout.timeout(5, loop=self.loop): await self.session.get(url) self._wifi_host = host return self._wifi_host # pylint: disable=W0703 except Exception: self._wifi_host = self._wifi_host return self._wifi_host
async def bluetooth_scan(): """Get nearby bluetooth devices.""" async with gdh_session() as session: bluetooth = Bluetooth(host, loop, session) await bluetooth.scan_for_devices() await gdh_sleep() await bluetooth.get_scan_result() print(format_json(bluetooth.devices))
async def scan_for_units(self, iprange): """Scan local network for Google devices.""" units = [] async with gdh_session() as session: googlewifi = WifiInfo(loop=self.loop, session=session) await googlewifi.get_wifi_info() if googlewifi.wifi_host is not None: wifi = { "assistant": False, "bluetooth": False, "host": googlewifi.wifi_host, "model": googlewifi.wifi_info.get("system", {}).get("modelId"), "name": "Google WiFi", } units.append(wifi) for host in ipaddress.IPv4Network(iprange): sock = socket.socket() sock.settimeout(0.2) host = str(host) try: scan_result = sock.connect((host, CASTPORT)) except socket.error: scan_result = 1 if scan_result is None: async with gdh_session() as session: googledevices = CastInfo(host, self.loop, session) await googledevices.get_device_info() data = googledevices.device_info if data is not None: info = data.get("device_info", {}) cap = info.get("capabilities", {}) units.append({ "host": host, "name": data.get("name"), "model": info.get("model_name"), "assistant": cap.get("assistant_supported", False), "bluetooth": cap.get("bluetooth_supported", False), }) sock.close() return units
async def get_all_units(): """Get device info for all Google devices.""" all_devices = [] if network is None: import netifaces gateway = netifaces.gateways().get("default", {}) subnet = gateway.get(netifaces.AF_INET, ())[0][:-1] + "0/24" else: subnet = network async with gdh_session() as session: googledevices = NetworkScan(loop, session) result = await googledevices.scan_for_units(subnet) if feature: for unit in result: if unit[feature]: all_devices.append(unit) else: all_devices = result print(format_json(all_devices))
async def set_alarm_volume(): """Get alarms and timers from GH.""" async with gdh_session() as session: googledevices = Assistant(host, loop, session) await googledevices.set_alarm_volume(volume)
async def test_all(): # pylint: disable=R0915 """Test all.""" print("Testing Cast.") print("Testing Cast - Assistant.") async with gdh_session() as session: print("Testing Cast - Assistant - get_alarms") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.get_alarms() print(format_json(test)) print("Testing Cast - Assistant - set_alarm_volume") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.set_alarm_volume(0.6) print(format_json(test)) print("Testing Cast - Assistant - get_alarm_volume") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.get_alarm_volume() print(format_json(test)) print("Testing Cast - Assistant - set_night_mode_params") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() data = {} test = await test_class.set_night_mode_params(data) print(format_json(test)) print("Testing Cast - Assistant - notifications_enabled") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.notifications_enabled() print(format_json(test)) print("Testing Cast - Assistant - set_accessibility") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.set_accessibility() print(format_json(test)) print("Testing Cast - Assistant - delete_alarms") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() data = [] test = await test_class.delete_alarms(data) print(format_json(test)) print("Testing Cast - Assistant - set_equalizer") test_class = await Cast(TEST_HOST_CAST, LOOP, session).assistant() test = await test_class.set_equalizer() print(format_json(test)) print("Testing Cast - Bluetooth.") print("Testing Cast - Bluetooth - get_bluetooth_status") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.get_bluetooth_status() print(format_json(test)) print("Testing Cast - Bluetooth - set_discovery_enabled") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.set_discovery_enabled() print(format_json(test)) print("Testing Cast - Bluetooth - scan_for_devices") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.scan_for_devices() print(format_json(test)) await gdh_sleep(2) print("Testing Cast - Bluetooth - get_scan_result") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.get_scan_result() print(format_json(test)) print("Testing Cast - Bluetooth - get_paired_devices") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.get_paired_devices() print(format_json(test)) print("Testing Cast - Bluetooth - pair_with_mac") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.pair_with_mac("AA:BB:CC:DD:EE:FF") print(format_json(test)) print("Testing Cast - Bluetooth - forget_paired_device") test_class = await Cast(TEST_HOST_CAST, LOOP, session).bluetooth() test = await test_class.forget_paired_device("AA:BB:CC:DD:EE:FF") print(format_json(test)) print("Testing Cast - Info.") print("Testing Cast - Info - get_device_info") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.get_device_info() print(format_json(test)) print("Testing Cast - Info - get_offer") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.get_offer() print(format_json(test)) print("Testing Cast - Info - get_timezones") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.get_timezones() print(format_json(test)) print("Testing Cast - Info - get_locales") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.get_locales() print(format_json(test)) print("Testing Cast - Info - speedtest") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.speedtest() print(format_json(test)) print("Testing Cast - Info - get_app_device_id") test_class = await Cast(TEST_HOST_CAST, LOOP, session).info() test = await test_class.get_app_device_id() print(format_json(test)) print("Testing Cast - Wifi.") print("Testing Cast - Wifi - get_configured_networks") test_class = await Cast(TEST_HOST_CAST, LOOP, session).wifi() test = await test_class.get_configured_networks() print(format_json(test)) print("Testing Cast - Wifi - scan_for_wifi") test_class = await Cast(TEST_HOST_CAST, LOOP, session).wifi() test = await test_class.scan_for_wifi() print(format_json(test)) await gdh_sleep(2) print("Testing Cast - Wifi - get_wifi_scan_result") test_class = await Cast(TEST_HOST_CAST, LOOP, session).wifi() test = await test_class.get_wifi_scan_result() print(format_json(test)) print("Testing Cast - Wifi - forget_network") test_class = await Cast(TEST_HOST_CAST, LOOP, session).wifi() test = await test_class.forget_network(9) print(format_json(test)) print("Testing Cast - Settings.") print("Testing Cast - Settings - control_notifications") test_class = await Cast(TEST_HOST_CAST, LOOP, session).settings() test = await test_class.control_notifications(True) print(format_json(test)) print("Testing Cast - Settings - set_eureka_info") test_class = await Cast(TEST_HOST_CAST, LOOP, session).settings() data = {"settings": {"control_notifications": 2}} test = await test_class.set_eureka_info(data) print(format_json(test)) print("Testing Cast - Settings - reboot") test_class = await Cast(TEST_HOST_CAST, LOOP, session).settings() test = await test_class.reboot() print(format_json(test)) print("Testing WiFi.") print("Testing WiFi - Info.") print("Testing WiFi - Info - get_host") test_class = await Wifi(TEST_HOST_WIFI, LOOP, session).info() test = await test_class.get_host() print(format_json(test)) print("Testing WiFi - Info - get_host - with host not defined.") test_class = await Wifi(loop=LOOP, session=session).info() test = await test_class.get_host() print(format_json(test)) print("Testing WiFi - Info - get_wifi_info") test_class = await Wifi(TEST_HOST_WIFI, LOOP, session).info() test = await test_class.get_wifi_info() print(format_json(test)) print("Testing WiFi - Clients.") print("Testing WiFi - Clients - get_clients") test_class = await Wifi(TEST_HOST_WIFI, LOOP, session).clients() test = await test_class.get_clients() print(format_json(test)) print("TESTS COMPLETE.")
async def connectivity(): """Test connectivity a Google Home unit.""" async with gdh_session(): googledevices = Debug(host) await googledevices.connectivity(timeout)
async def get_device_info(): """Get device info.""" async with gdh_session() as session: googledevices = Info(host, loop, session) await googledevices.get_device_info() print(format_json(googledevices.device_info))