Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy & R2 #2439

Merged
merged 44 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
006ca1c
Entity
andrew-codechimp Nov 11, 2024
6f0a5bd
Description class inheritance
andrew-codechimp Nov 11, 2024
9a222f5
Duplicate definition
andrew-codechimp Nov 11, 2024
30423d5
Attributes
andrew-codechimp Nov 11, 2024
1176323
Missing service return
andrew-codechimp Nov 11, 2024
bffc968
Service param typing
andrew-codechimp Nov 11, 2024
f375ef6
Entity registry updated
andrew-codechimp Nov 11, 2024
1914c77
Template sensor
andrew-codechimp Nov 11, 2024
434998f
Config flow title
andrew-codechimp Nov 11, 2024
5e35814
Store
andrew-codechimp Nov 12, 2024
b42631f
Merge remote-tracking branch 'origin/main' into mypy
andrew-codechimp Nov 12, 2024
108dbb9
init
andrew-codechimp Nov 12, 2024
3ff0f6d
Repairs
andrew-codechimp Nov 12, 2024
0c913b8
Typing
andrew-codechimp Nov 12, 2024
4c20367
Typing
andrew-codechimp Nov 12, 2024
7bd2808
Typing
andrew-codechimp Nov 12, 2024
267c7c6
Check wrapped_battery
andrew-codechimp Nov 15, 2024
3d24606
Typing
andrew-codechimp Nov 15, 2024
328740a
Add dev version suffix to non github packaged versions
andrew-codechimp Nov 16, 2024
c5c1deb
Bump HA min version to 2024.10
andrew-codechimp Nov 16, 2024
5d75ea0
Merge branch 'main' into mypy
andrew-codechimp Nov 20, 2024
7e0e1b8
Update device: Smart door and window sensor (ZSS-JM-GWM-C-MS) by Moes…
github-actions[bot] Nov 20, 2024
e3d08d7
Apply automatic changes
andrew-codechimp Nov 20, 2024
b293043
Update events documentation
andrew-codechimp Nov 21, 2024
5b9edb4
Fix doc links
andrew-codechimp Nov 21, 2024
d0c0620
Fix docs links
andrew-codechimp Nov 21, 2024
e2cec76
New Crowdin translations by GitHub Action (#2420)
github-actions[bot] Nov 21, 2024
d5c8e5a
Update device: MCCGQ02HL by Xiaomi (#2422)
github-actions[bot] Nov 21, 2024
7a5dc8e
Apply automatic changes
andrew-codechimp Nov 21, 2024
584936b
New Crowdin translations by GitHub Action (#2424)
github-actions[bot] Nov 22, 2024
fdd53c2
New Crowdin translations by GitHub Action (#2425)
github-actions[bot] Nov 22, 2024
d828490
Adjusting the battery type for the QingPing CGG1 temp sensor (#2427)
jzucker2 Nov 23, 2024
70a2a5e
New Crowdin translations by GitHub Action (#2429)
github-actions[bot] Nov 23, 2024
5bba9c2
Update device: Model A by Waveshare (#2431)
github-actions[bot] Nov 23, 2024
713889e
Apply automatic changes
andrew-codechimp Nov 23, 2024
46bec51
Update device: Meter by SwitchBot (#2433)
github-actions[bot] Nov 23, 2024
90b4681
Apply automatic changes
andrew-codechimp Nov 23, 2024
48137bb
New Crowdin translations by GitHub Action (#2436)
github-actions[bot] Nov 24, 2024
c87e1f0
Device: Ecowitt - GW2001 (#2435)
github-actions[bot] Nov 24, 2024
8c41704
Update device: Duette by Hunter Douglas by Hunter Douglas (#2438)
github-actions[bot] Nov 24, 2024
80d393f
Apply automatic changes
andrew-codechimp Nov 24, 2024
a1ad05f
Typing
andrew-codechimp Nov 12, 2024
2621c4c
Fix library url
andrew-codechimp Nov 24, 2024
3169971
Change to R2 URL
andrew-codechimp Nov 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.data[DOMAIN][DATA_LIBRARY_UPDATER] = library_updater

if domain_config.get(CONF_ENABLE_AUTODISCOVERY):
discovery_manager = DiscoveryManager(hass, config)
discovery_manager = DiscoveryManager(hass, domain_config)
await discovery_manager.start_discovery()
else:
_LOGGER.debug("Auto discovery disabled")
Expand Down Expand Up @@ -181,7 +181,7 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return

device: BatteryNotesDevice = hass.data[DOMAIN][DATA].devices[config_entry.entry_id]
if not device:
if not device or not device.coordinator.device_id:
return

data = {ATTR_REMOVE: True}
Expand Down Expand Up @@ -221,7 +221,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
# Version 1 had a single config for qty & type, split them
_LOGGER.debug("Migrating config entry from version %s", config_entry.version)

matches: re.Match = re.search(
matches = re.search(
r"^(\d+)(?=x)(?:x\s)(\w+$)|([\s\S]+)", config_entry.data[CONF_BATTERY_TYPE]
)
if matches:
Expand Down
11 changes: 6 additions & 5 deletions custom_components/battery_notes/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
_LOGGER = logging.getLogger(__name__)


@dataclass
@dataclass(frozen=True, kw_only=True)
class BatteryNotesBinarySensorEntityDescription(
BatteryNotesEntityDescription,
BinarySensorEntityDescription,
Expand Down Expand Up @@ -123,7 +123,7 @@ async def async_setup_entry(

device_id = config_entry.data.get(CONF_DEVICE_ID)

async def async_registry_updated(event: Event) -> None:
async def async_registry_updated(event: Event[er.EventEntityRegistryUpdatedData]) -> None:
"""Handle entity registry update."""
data = event.data
if data["action"] == "remove":
Expand Down Expand Up @@ -177,8 +177,6 @@ async def async_registry_updated(event: Event) -> None:
device_class=BinarySensorDeviceClass.BATTERY,
)

device: BatteryNotesDevice = hass.data[DOMAIN][DATA].devices[config_entry.entry_id]

if coordinator.battery_low_template is not None:
async_add_entities(
[
Expand Down Expand Up @@ -314,6 +312,7 @@ class BatteryNotesBatteryLowTemplateSensor(
"""Represents a low battery threshold binary sensor."""

_attr_should_poll = False
_self_ref_update_count = 0

def __init__(
self,
Expand Down Expand Up @@ -587,6 +586,8 @@ def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""

if (
not self.coordinator.wrapped_battery
or
(
wrapped_battery_state := self.hass.states.get(
self.coordinator.wrapped_battery.entity_id
Expand Down Expand Up @@ -616,7 +617,7 @@ def _handle_coordinator_update(self) -> None:
)

@property
def extra_state_attributes(self) -> dict[str, str] | None:
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes of battery low."""

attrs = {
Expand Down
4 changes: 2 additions & 2 deletions custom_components/battery_notes/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
_LOGGER = logging.getLogger(__name__)


@dataclass
@dataclass(frozen=True, kw_only=True)
class BatteryNotesButtonEntityDescription(
BatteryNotesEntityDescription,
ButtonEntityDescription,
Expand Down Expand Up @@ -104,7 +104,7 @@ async def async_setup_entry(

device_id = config_entry.data.get(CONF_DEVICE_ID, None)

async def async_registry_updated(event: Event) -> None:
async def async_registry_updated(event: Event[er.EventEntityRegistryUpdatedData]) -> None:
"""Handle entity registry update."""
data = event.data
if data["action"] == "remove":
Expand Down
5 changes: 3 additions & 2 deletions custom_components/battery_notes/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,12 @@ async def save_options(
errors["base"] = "orphaned_battery_note"
return errors

title: Any = ""
if CONF_NAME in user_input:
title = user_input.get(CONF_NAME)
elif source_entity_id:
elif source_entity_id and entity_entry:
title = entity_entry.name or entity_entry.original_name
else:
elif device_entry:
title = device_entry.name_by_user or device_entry.name

self._process_user_input(user_input, schema)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/battery_notes/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

LOGGER: Logger = getLogger(__package__)

MIN_HA_VERSION = "2024.9"
MIN_HA_VERSION = "2024.10"

manifestfile = Path(__file__).parent / "manifest.json"
with open(file=manifestfile, encoding="UTF-8") as json_file:
Expand All @@ -30,7 +30,7 @@

DEFAULT_BATTERY_LOW_THRESHOLD = 10
DEFAULT_BATTERY_INCREASE_THRESHOLD = 25
DEFAULT_LIBRARY_URL = "https://raw.githubusercontent.com/andrew-codechimp/HA-Battery-Notes/main/custom_components/battery_notes/data/library.json" # pylint: disable=line-too-long
DEFAULT_LIBRARY_URL = "https://battery-notes-data.codechimp.org/library.json"

CONF_SOURCE_ENTITY_ID = "source_entity_id"
CONF_BATTERY_TYPE = "battery_type"
Expand Down
22 changes: 11 additions & 11 deletions custom_components/battery_notes/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@
class BatteryNotesCoordinator(DataUpdateCoordinator):
"""Define an object to hold Battery Notes device."""

device_id: str = None
source_entity_id: str = None
device_id: str | None = None
source_entity_id: str | None = None
device_name: str
battery_type: str
battery_quantity: int
battery_low_threshold: int
battery_low_template: str | None
wrapped_battery: RegistryEntry
_current_battery_level: str = None
wrapped_battery: RegistryEntry | None = None
_current_battery_level: str | None = None
enable_replaced: bool = True
_round_battery: bool = False
_previous_battery_low: bool = None
_previous_battery_level: str = None
_previous_battery_low: bool | None = None
_previous_battery_level: str | None = None
_battery_low_template_state: bool = False
_previous_battery_low_template_state: bool = None
_source_entity_name: str = None
_previous_battery_low_template_state: bool | None = None
_source_entity_name: str | None = None

def __init__(
self, hass, store: BatteryNotesStorage, wrapped_battery: RegistryEntry
self, hass, store: BatteryNotesStorage, wrapped_battery: RegistryEntry | None
):
"""Initialize."""
self.store = store
Expand Down Expand Up @@ -242,13 +242,13 @@ def last_replaced(self) -> datetime | None:
return None

@last_replaced.setter
def last_replaced(self, value):
def last_replaced(self, value: datetime):
"""Set the last replaced datetime and store it."""
entry = {LAST_REPLACED: value}

if self.source_entity_id:
self.async_update_entity_config(entity_id=self.source_entity_id, data=entry)
else:
elif self.device_id:
self.async_update_device_config(device_id=self.device_id, data=entry)

@property
Expand Down
35 changes: 34 additions & 1 deletion custom_components/battery_notes/data/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,11 @@
"model": "GW2000A",
"battery_type": "MANUAL"
},
{
"manufacturer": "Ecowitt",
"model": "GW2001",
"battery_type": "MANUAL"
},
{
"manufacturer": "ECOWITT",
"model": "WH51",
Expand Down Expand Up @@ -2781,6 +2786,11 @@
"battery_type": "AA",
"battery_quantity": 12
},
{
"manufacturer": "Hunter Douglas",
"model": "Duette by Hunter Douglas",
"battery_type": "Rechargeable"
},
{
"manufacturer": "Hunter Douglas",
"model": "Vertical Slats, Left Stack",
Expand Down Expand Up @@ -3572,6 +3582,12 @@
"model": "kvwjujy9",
"battery_type": "CR2032"
},
{
"manufacturer": "Moes",
"model": "Smart door and window sensor (ZSS-JM-GWM-C-MS)",
"model_id": "_TZ3000_decxrtwa",
"battery_type": "CR2032"
},
{
"manufacturer": "Moes",
"model": "Smoke sensor (ZSS-HM-SSD01)",
Expand Down Expand Up @@ -4174,7 +4190,7 @@
{
"manufacturer": "Qingping",
"model": "CGG1",
"battery_type": "CR2032"
"battery_type": "CR2430"
},
{
"manufacturer": "Qingping Technology (Beijing) Co., Ltd.",
Expand Down Expand Up @@ -5183,6 +5199,12 @@
"battery_quantity": 4,
"model_match_method": "startswith"
},
{
"manufacturer": "SwitchBot",
"model": "Meter",
"battery_type": "AAA",
"battery_quantity": 2
},
{
"manufacturer": "SwitchBot",
"model": "W1101500",
Expand Down Expand Up @@ -6335,6 +6357,12 @@
"model": "Next K9-85",
"battery_type": "CR123A"
},
{
"manufacturer": "Waveshare",
"model": "Model A",
"battery_type": "18650",
"battery_quantity": 2
},
{
"manufacturer": "WAXMAN",
"model": "leakSMART Water Sensor V2",
Expand Down Expand Up @@ -6678,6 +6706,11 @@
"model": "MCCGQ01LM",
"battery_type": "CR1632"
},
{
"manufacturer": "Xiaomi",
"model": "MCCGQ02HL",
"battery_type": "CR2032"
},
{
"manufacturer": "Xiaomi",
"model": "MHO-C303",
Expand Down
17 changes: 9 additions & 8 deletions custom_components/battery_notes/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
from datetime import datetime
from typing import cast

from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.sensor import (
Expand Down Expand Up @@ -41,11 +42,11 @@
class BatteryNotesDevice:
"""Manages a Battery Note device."""

config: ConfigEntry = None
store: BatteryNotesStorage = None
coordinator: BatteryNotesCoordinator = None
wrapped_battery: RegistryEntry = None
device_name: str = None
config: ConfigEntry
store: BatteryNotesStorage
coordinator: BatteryNotesCoordinator
wrapped_battery: RegistryEntry | None = None
device_name: str | None = None

def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None:
"""Initialize the device."""
Expand Down Expand Up @@ -205,9 +206,9 @@ async def async_setup(self) -> bool:
self.coordinator.device_id = device_id
self.coordinator.source_entity_id = source_entity_id
self.coordinator.device_name = self.device_name
self.coordinator.battery_type = config.data.get(CONF_BATTERY_TYPE)
self.coordinator.battery_type = cast(str, config.data.get(CONF_BATTERY_TYPE))
try:
self.coordinator.battery_quantity = int(
self.coordinator.battery_quantity = cast(int,
config.data.get(CONF_BATTERY_QUANTITY)
)
except ValueError:
Expand Down Expand Up @@ -255,7 +256,7 @@ async def async_setup(self) -> bool:
last_replaced,
)

self.coordinator.last_replaced = last_replaced
self.coordinator.last_replaced = datetime.fromisoformat(last_replaced) if last_replaced else None

# If there is not a last_reported set to now
if not self.coordinator.last_reported:
Expand Down
9 changes: 5 additions & 4 deletions custom_components/battery_notes/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ async def async_get_config_entry_diagnostics(
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""

device_id = config_entry.data.get(CONF_DEVICE_ID, None)
source_entity_id = config_entry.data.get(CONF_SOURCE_ENTITY_ID, None)

device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)

device_id = config_entry.data.get(CONF_DEVICE_ID, None)
source_entity_id = config_entry.data.get(CONF_SOURCE_ENTITY_ID, None)

if source_entity_id:
entity = entity_registry.async_get(source_entity_id)
device_id = entity.device_id
if entity:
device_id = entity.device_id

diagnostics = {"entry": config_entry.as_dict()}

Expand Down
4 changes: 2 additions & 2 deletions custom_components/battery_notes/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def autodiscover_model(

async def get_model_information(
device_entry: dr.DeviceEntry,
) -> DeviceBatteryDetails | None:
) -> ModelInfo | None:
"""See if we have enough information to automatically setup the battery type."""

manufacturer = device_entry.manufacturer
Expand Down Expand Up @@ -129,7 +129,7 @@ def should_process_device(self, device_entry: dr.DeviceEntry) -> bool:
def _init_entity_discovery(
self,
device_entry: dr.DeviceEntry,
device_battery_details: DeviceBatteryDetails | None,
device_battery_details: DeviceBatteryDetails,
) -> None:
"""Dispatch the discovery flow for a given entity."""
existing_entries = [
Expand Down
4 changes: 2 additions & 2 deletions custom_components/battery_notes/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from homeassistant.helpers.entity import EntityDescription


@dataclass
@dataclass(frozen=True, kw_only=True)
class BatteryNotesRequiredKeysMixin:
"""Mixin for required keys."""

unique_id_suffix: str


@dataclass
@dataclass(frozen=True, kw_only=True)
class BatteryNotesEntityDescription(EntityDescription, BatteryNotesRequiredKeysMixin):
"""Generic Battery Notes entity description."""
2 changes: 1 addition & 1 deletion custom_components/battery_notes/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class Library: # pylint: disable=too-few-public-methods
"""Hold all known battery types."""

_devices = []
_devices: list = []

def __init__(self, hass: HomeAssistant) -> None:
"""Init."""
Expand Down
Loading