Skip to content

Commit

Permalink
feat: Update to require Hass 2024.4+ and Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sopelj committed Jun 23, 2024
1 parent c6e52b8 commit 38a12b1
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 141 deletions.
20 changes: 10 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
default_language_version:
python: python3.10
python: python3.12
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
rev: v3.16.0
hooks:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
rev: v3.1.0
hooks:
- id: add-trailing-comma
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.1
rev: v1.5.5
hooks:
- id: forbid-crlf
- id: remove-crlf
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -29,16 +29,16 @@ repos:
- id: isort
args: [ "--filter-files", "--profile", "black" ]
- repo: https://github.com/ambv/black
rev: 23.3.0
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.270
rev: v0.4.10
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
rev: v2.3.0
hooks:
- id: codespell
args:
Expand All @@ -47,7 +47,7 @@ repos:
- --quiet-level=2
exclude_types: [csv, json]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
rev: 1.7.9
hooks:
- id: bandit
args:
Expand All @@ -56,7 +56,7 @@ repos:
- --configfile=tests/bandit.yaml
types: [ python ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.10.0
hooks:
- id: mypy
args:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [1.1.0]

### Changes
* Upgrade to python-ember-mug 1.0.1
* Move data to `entry.runtime_data` introduced in Home Assistant 2024.4

### Notes
* Requires Home Assistant 2024.4+ and Python 3.12

## [1.0.0]

# Added
Expand Down
19 changes: 8 additions & 11 deletions custom_components/ember_mug/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Ember Mug Custom Integration."""

from __future__ import annotations

import asyncio
Expand All @@ -24,14 +25,16 @@

from .const import CONF_DEBUG, CONFIG_VERSION, DOMAIN
from .coordinator import MugDataUpdateCoordinator
from .models import HassMugData

if TYPE_CHECKING:
from home_assistant_bluetooth import BluetoothServiceInfoBleak
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Event, HomeAssistant


type EmberMugConfigEntry = ConfigEntry[MugDataUpdateCoordinator]


PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.LIGHT,
Expand All @@ -46,9 +49,6 @@

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Mug Platform."""
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}

address: str = entry.data[CONF_ADDRESS].upper()
service_info = bluetooth.async_last_service_info(hass, address, connectable=True)

Expand Down Expand Up @@ -135,10 +135,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
),
)

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HassMugData(
ember_mug,
mug_coordinator,
)
entry.runtime_data = mug_coordinator
entry.async_on_unload(entry.add_update_listener(async_update_listener))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down Expand Up @@ -192,12 +189,12 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: EmberMugConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass_mug_data: HassMugData = hass.data[DOMAIN].pop(entry.entry_id)
await hass_mug_data.coordinator.mug.disconnect()
mug_coordinator = entry.runtime_data
await mug_coordinator.mug.disconnect()
if not hass.config_entries.async_entries(DOMAIN):
hass.data.pop(DOMAIN)

Expand Down
15 changes: 7 additions & 8 deletions custom_components/ember_mug/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Binary Sensor Entity for Ember Mug."""

from __future__ import annotations

import logging
Expand All @@ -10,18 +11,16 @@
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.helpers.entity import EntityCategory
from homeassistant.const import EntityCategory

from .const import DOMAIN
from .entity import BaseMugEntity

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import EmberMugConfigEntry
from .coordinator import MugDataUpdateCoordinator
from .models import HassMugData


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -81,16 +80,16 @@ def is_on(self) -> bool | None:

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: EmberMugConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Binary Sensor Entities."""
if entry.entry_id is None:
raise ValueError("Missing Entry ID")
data: HassMugData = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data
async_add_entities(
[
MugBinarySensor(data.coordinator, "battery.on_charging_base"),
MugLowBatteryBinarySensor(data.coordinator, "battery.percent"),
MugBinarySensor(coordinator, "battery.on_charging_base"),
MugLowBatteryBinarySensor(coordinator, "battery.percent"),
],
)
1 change: 1 addition & 0 deletions custom_components/ember_mug/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Add Config Flow for Ember Mug."""

from __future__ import annotations

import contextlib
Expand Down
1 change: 1 addition & 0 deletions custom_components/ember_mug/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Constants used for mug."""

from enum import StrEnum
from typing import Final

Expand Down
6 changes: 3 additions & 3 deletions custom_components/ember_mug/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Coordinator for all the sensors."""

from __future__ import annotations

import asyncio
import logging
from datetime import timedelta
from typing import TYPE_CHECKING, Any, TypedDict
Expand Down Expand Up @@ -102,11 +102,11 @@ async def _async_update_data(self) -> MugData:
changed += await self.mug.update_queued_attributes()
self._last_refresh_was_full = not self._last_refresh_was_full
self.available = True
except (asyncio.TimeoutError, BleakError) as e:
except (TimeoutError, BleakError) as e:
if isinstance(e, BleakError):
_LOGGER.debug("An error occurred trying to update the mug: %s", e)
if self.available is True:
_LOGGER.debug("%s is not available: %s", e)
_LOGGER.debug("%s is not available: %s", self.mug.model_name, e)
self.available = False
if self._initial_update is True:
raise UpdateFailed(
Expand Down
11 changes: 4 additions & 7 deletions custom_components/ember_mug/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
"""Diagnostics support for Mug."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any

from bleak import BleakError

from .const import DOMAIN

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from . import HassMugData
from . import EmberMugConfigEntry


logger = logging.getLogger(__name__)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
entry: ConfigEntry,
entry: EmberMugConfigEntry,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
hass_data: HassMugData = hass.data[DOMAIN][entry.entry_id]
coordinator = hass_data.coordinator
coordinator = entry.runtime_data
data: dict[str, Any] = {
"info": coordinator.data,
"state": coordinator.data.liquid_state_display,
Expand Down
1 change: 1 addition & 0 deletions custom_components/ember_mug/entity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generic Entity Logic for multiple platforms."""

from __future__ import annotations

import logging
Expand Down
16 changes: 7 additions & 9 deletions custom_components/ember_mug/light.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Expose the Mug's LEDs as a light entity."""

from __future__ import annotations

import logging
Expand All @@ -15,16 +16,13 @@
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory

from .const import DOMAIN
from .entity import BaseMugEntity

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .models import HassMugData

from . import EmberMugConfigEntry

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,7 +53,7 @@ def _async_update_attrs(self) -> None:

async def async_turn_on(self, **kwargs: Any) -> None:
"""Change the LED colour if defined."""
_LOGGER.debug(f"Received turn on with {kwargs}")
_LOGGER.debug("Received turn on with %s", kwargs)
self.coordinator.ensure_writable()
current_colour = self.coordinator.mug.data.led_colour
rgb: tuple[int, int, int]
Expand All @@ -80,14 +78,14 @@ def turn_off(self, **kwargs: Any) -> None:

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: EmberMugConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the mug light."""
if entry.entry_id is None:
raise ValueError("Missing config entry ID")
data: HassMugData = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data
entities = []
if data.mug.has_attribute("led_colour"):
entities = [MugLightEntity(data.coordinator, "led_colour")]
if coordinator.mug.has_attribute("led_colour"):
entities = [MugLightEntity(coordinator, "led_colour")]
async_add_entities(entities)
18 changes: 0 additions & 18 deletions custom_components/ember_mug/models.py

This file was deleted.

12 changes: 6 additions & 6 deletions custom_components/ember_mug/number.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Binary Sensor Entity for Ember Mug."""

from __future__ import annotations

import logging
Expand All @@ -13,16 +14,15 @@
from homeassistant.const import UnitOfTemperature
from homeassistant.helpers.entity import EntityCategory

from .const import DOMAIN, MAX_TEMP_CELSIUS, MIN_TEMP_CELSIUS
from .const import MAX_TEMP_CELSIUS, MIN_TEMP_CELSIUS
from .entity import BaseMugValueEntity

if TYPE_CHECKING:
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import EmberMugConfigEntry
from .coordinator import MugDataUpdateCoordinator
from .models import HassMugData


_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,15 +73,15 @@ async def async_set_native_value(self, value: float) -> None:

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: EmberMugConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Number Entities."""
if entry.entry_id is None:
raise ValueError("Missing config entry ID")
data: HassMugData = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data
async_add_entities(
[
MugTargetTempNumberEntity(data.coordinator, "target_temp"),
MugTargetTempNumberEntity(coordinator, "target_temp"),
],
)
Loading

0 comments on commit 38a12b1

Please sign in to comment.