Skip to content

Commit

Permalink
update HA to 2023.11, Python to 3.11, linting and refactoring (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Nov 3, 2023
1 parent 5c8701a commit 0ca51b6
Show file tree
Hide file tree
Showing 14 changed files with 1,483 additions and 1,184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/home-assistant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
pull_request:

env:
DEFAULT_PYTHON: "3.10"
DEFAULT_PYTHON: "3.11"

jobs:
hacs:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ on:
- master
pull_request:

env:
PYTHON_VERSION: "3.11"

jobs:
pre-commit:
name: Pre-commit
strategy:
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Poetry
uses: abatilo/actions-poetry@v2.3.0
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ name: Publish new release
on:
workflow_dispatch:

env:
PYTHON_VERSION: "3.11"

jobs:
publish:
name: Update manifest and publish
strategy:
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Poetry
uses: abatilo/actions-poetry@v2.3.0
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ When writing unittests please follow the good practises like:
## Pre-commit

With Poetry installed, run `poetry install` in the repo root.
It will create a virualenv with all required packages.
It will create a virtualenv with all required packages.

After that you can run [pre-commit](https://pre-commit.com/) with settings included in the
repository to have code style and linting checks.
Expand Down
7 changes: 4 additions & 3 deletions custom_components/google_home/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
STARTUP_MESSAGE,
UPDATE_INTERVAL,
)
from .models import GoogleHomeDevice

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -94,9 +95,9 @@ async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update config entry."""
_LOGGER.debug("Updating entry...")
update_interval: int = entry.options.get(CONF_UPDATE_INTERVAL, UPDATE_INTERVAL)
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
coordinator: DataUpdateCoordinator[list[GoogleHomeDevice]] = hass.data[DOMAIN][
entry.entry_id
][DATA_COORDINATOR]
coordinator.update_interval = timedelta(seconds=update_interval)
_LOGGER.debug(
"Coordinator update interval is: %s", timedelta(seconds=update_interval)
Expand Down
12 changes: 6 additions & 6 deletions custom_components/google_home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ async def update_do_not_disturb(
return device

async def update_alarm_volume(
self, device: GoogleHomeDevice, volume: float | None = None
self, device: GoogleHomeDevice, volume: int | None = None
) -> GoogleHomeDevice:
"""Gets or sets the alarm volume setting on a Google Home device."""

Expand All @@ -330,7 +330,7 @@ async def update_alarm_volume(

if volume is not None:
# Setting is inverted on device
volume_float = float(volume / 100)
volume_float = volume / 100
data = {JSON_ALARM_VOLUME: volume_float}
_LOGGER.debug(
"Setting alarm volume to %d(float=%f) on Google Home device %s",
Expand All @@ -356,23 +356,23 @@ async def update_alarm_volume(
if JSON_ALARM_VOLUME in response:
if polling:
volume_raw = str(response[JSON_ALARM_VOLUME])
volume_int = round(float(volume_raw) * 100)
volume = round(float(volume_raw) * 100)
_LOGGER.debug(
"Received alarm volume from Google Home device %s"
" - Volume: %d(raw=%s)",
device.name,
volume_int,
volume,
volume_raw,
)
else:
volume_int = volume # type: ignore
assert volume is not None
_LOGGER.debug(
"Successfully set alarm volume to %d "
"on Google Home device %s",
volume,
device.name,
)
device.set_alarm_volume(volume_int)
device.set_alarm_volume(volume)
else:
_LOGGER.debug(
(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/google_home/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self) -> None:
self._errors: dict[str, str] = {}

async def async_step_user(
self, user_input: ConfigFlowDict | None = None
self, user_input: ConfigFlowDict | None = None # type: ignore[override]
) -> FlowResult:
"""Handle a flow initialized by the user."""
self._errors = {}
Expand Down
10 changes: 6 additions & 4 deletions custom_components/google_home/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from abc import ABC, abstractmethod

from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand All @@ -11,15 +12,16 @@
from .api import GlocaltokensApiClient
from .const import DEFAULT_NAME, DOMAIN, MANUFACTURER
from .models import GoogleHomeDevice
from .types import DeviceInfo


class GoogleHomeBaseEntity(CoordinatorEntity[DataUpdateCoordinator], ABC):
class GoogleHomeBaseEntity(
CoordinatorEntity[DataUpdateCoordinator[list[GoogleHomeDevice]]], ABC
):
"""Base entity base for Google Home sensors"""

def __init__(
self,
coordinator: DataUpdateCoordinator,
coordinator: DataUpdateCoordinator[list[GoogleHomeDevice]],
client: GlocaltokensApiClient,
device_id: str,
device_name: str,
Expand Down Expand Up @@ -47,7 +49,7 @@ def unique_id(self) -> str:
return f"{self.device_id}/{self.label}"

@property
def device_info(self) -> DeviceInfo:
def device_info(self) -> DeviceInfo | None:
return {
"identifiers": {(DOMAIN, self.device_id)},
"name": f"{DEFAULT_NAME} {self.device_name}",
Expand Down
9 changes: 5 additions & 4 deletions custom_components/google_home/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
LABEL_ALARM_VOLUME,
)
from .entity import GoogleHomeBaseEntity
from .models import GoogleHomeDevice

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -35,9 +36,9 @@ async def async_setup_entry(
) -> bool:
"""Setup switch platform."""
client: GlocaltokensApiClient = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
coordinator: DataUpdateCoordinator[list[GoogleHomeDevice]] = hass.data[DOMAIN][
entry.entry_id
][DATA_COORDINATOR]

numbers: list[NumberEntity] = []
for device in coordinator.data:
Expand Down Expand Up @@ -101,4 +102,4 @@ async def async_set_native_value(self, value: float) -> None:
_LOGGER.error("Device %s not found.", self.device_name)
return

await self.client.update_alarm_volume(device=device, volume=value)
await self.client.update_alarm_volume(device=device, volume=round(value))
35 changes: 18 additions & 17 deletions custom_components/google_home/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,24 @@ async def async_setup_entry(
platform = entity_platform.current_platform.get()

# Services
platform.async_register_entity_service(
SERVICE_DELETE_ALARM,
{vol.Required(SERVICE_ATTR_ALARM_ID): cv.string},
"async_delete_alarm",
)

platform.async_register_entity_service(
SERVICE_DELETE_TIMER,
{vol.Required(SERVICE_ATTR_TIMER_ID): cv.string},
"async_delete_timer",
)

platform.async_register_entity_service(
SERVICE_REBOOT,
{},
"async_reboot_device",
)
if platform is not None:
platform.async_register_entity_service(
SERVICE_DELETE_ALARM,
vol.Schema({vol.Required(SERVICE_ATTR_ALARM_ID): cv.string}),
GoogleHomeAlarmsSensor.async_delete_alarm.__name__,
)

platform.async_register_entity_service(
SERVICE_DELETE_TIMER,
vol.Schema({vol.Required(SERVICE_ATTR_TIMER_ID): cv.string}),
GoogleHomeTimersSensor.async_delete_timer.__name__,
)

platform.async_register_entity_service(
SERVICE_REBOOT,
vol.Schema({}),
GoogleHomeDeviceSensor.async_reboot_device.__name__,
)

return True

Expand Down
3 changes: 3 additions & 0 deletions custom_components/google_home/services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Services.yaml for google_home integration

reboot_device:
name: Reboot device
description: Reboot a Google Home device.
fields:
entity_id:
Expand All @@ -12,6 +13,7 @@ reboot_device:
text:

delete_alarm:
name: Delete alarm
description: Delete an alarm from a Google Home device.
fields:
entity_id:
Expand All @@ -30,6 +32,7 @@ delete_alarm:
text:

delete_timer:
name: Delete timer
description: Delete a timer from a Google Home device.
fields:
entity_id:
Expand Down
9 changes: 0 additions & 9 deletions custom_components/google_home/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ class TimersAttributes(TypedDict):
timers: list[GoogleHomeTimerDict]


class DeviceInfo(TypedDict):
"""Typed dict for device_info"""

identifiers: set[tuple[str, str]]
name: str
manufacturer: str
model: str


class ConfigFlowDict(TypedDict):
"""Typed dict for config flow handler"""

Expand Down
Loading

0 comments on commit 0ca51b6

Please sign in to comment.