Skip to content

v0.1.0

Latest

Choose a tag to compare

@github-actions github-actions released this 08 Nov 01:15
· 13 commits to main since this release
Immutable release. Only release title and notes can be modified.
6974970

ghome-local-api 0.1.0 is the first release: an asynchronous, typed Python client for the local API of Google Home devices. It was moved out of ha-google-home so that it and other projects can share one implementation.

🚀 What's in it

  • Talk to a device over your own aiohttp session. GoogleHomeLocalClient(session) sends one request per call. It reads and deletes alarms and timers, reads and sets the alarm volume and Do Not Disturb, reboots a device, and starts a Bluetooth scan and reads what it heard. Each call takes the device's IP address, its local auth token if the call needs one, a timeout (5 seconds by default) and an optional on_response, which sees the status before the body is read. The client keeps no state, holds no token, retries nothing and doesn't log. See Calls. (#4)
  • Device, build and update info without a token, such as the device's name, firmware version, uptime and Wi-Fi signal level. (#4)
  • Typed replies. Calls return frozen dataclasses such as AlarmsAndTimers, AlarmVolumeReply and BluetoothSighting, and the package is marked as typed. An alarm or timer status the package doesn't know is UNKNOWN. An alarms reply that can't be read in full raises UnexpectedResponseError instead of returning part of it. (#4)
  • Errors you can act on. Every error derives from GoogleHomeLocalError and has host and path. For example, TokenRejectedError means you need a new token, CannotConnectError that the connection failed, and AnswerTimeoutError that the device didn't answer within timeout. JSON that can't be decoded raises InvalidJsonError, including a reply whose charset isn't a text encoding; the info calls return None instead. Error messages never include the token or what the device sent. See Errors. (#4, #7, #8)
  • Works with Home Assistant's test mock. Responses without a reason, such as those from pytest-homeassistant's aioclient_mock, work like real ones. (#7)
import aiohttp

from ghome_local_api import GoogleHomeLocalClient, TokenRejectedError


async def print_timers(host: str, token: str) -> None:
    # The devices use self-signed certificates
    connector = aiohttp.TCPConnector(ssl=False)
    async with aiohttp.ClientSession(connector=connector) as session:
        client = GoogleHomeLocalClient(session)
        try:
            reply = await client.get_alarms_and_timers(host, token)
        except TokenRejectedError:
            print("The device rejected the token: get a new one with glocaltokens")
            return
        for timer in reply.timers:
            print(timer.id, timer.status.name, timer.remaining_duration)

⚠️ Before you use it

  • These are unofficial endpoints. Google doesn't document them and can change them in a firmware update. Not every device has every endpoint. One that doesn't usually answers 404, which raises NotFoundError, but some refuse the connection instead.
  • Get tokens with glocaltokens. This package doesn't get them. The info calls need no token.
  • ssl=False turns off certificate checks. It accepts the device's self-signed certificate, but then a device on your network can't be told from an impostor that receives the token. Use it only on a network you trust. See Session requirements.
  • Don't log errors or replies whole. The aiohttp exception in __cause__ can hold the token header. Replies and StatusError.reason hold what the device sent, such as alarm labels. A redirect that aiohttp follows sends the token header to the new location. See Sensitive data.
  • Versions before 1.0.0 may still change the API. ha-google-home doesn't use the package yet.

📦 Install

$ pip install ghome-local-api

It needs Python 3.12 or newer and aiohttp 3.14.4 or newer. The files are uploaded from GitHub Actions through trusted publishing, with PEP 740 attestations, so PyPI shows verified provenance for each file. (#9)

🔧 Under the hood

  • CI runs the tests on Python 3.12 and 3.14, and with the lowest allowed dependencies. The tests run the client against a fake device on a local TLS server. Coverage is reported to Codecov. (#1, #4, #6)

Full changelog: https://github.com/KapJI/ghome-local-api/commits/v0.1.0