Skip to content

Commit

Permalink
Merge branch 'main' into 202409-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed Oct 27, 2024
2 parents cee2ff1 + 858c914 commit 81fcac1
Show file tree
Hide file tree
Showing 14 changed files with 1,442 additions and 1,289 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/integration/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BN Integration",
"image": "mcr.microsoft.com/vscode/devcontainers/python:3.12-bullseye",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"postCreateCommand": "scripts/setup",
"runArgs": [
"--network=host"
Expand Down
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml

target-version = "py310"
target-version = "py312"

[lint]
select = [
Expand Down
50 changes: 40 additions & 10 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
DOMAIN,
DOMAIN_CONFIG,
EVENT_BATTERY_NOT_REPORTED,
EVENT_BATTERY_REPLACED,
EVENT_BATTERY_THRESHOLD,
MIN_HA_VERSION,
PLATFORMS,
Expand Down Expand Up @@ -306,11 +307,7 @@ async def handle_battery_replaced(call):
hass.data[DOMAIN][DATA].devices[config_entry_id].coordinator
)

entry = {"battery_last_replaced": datetime_replaced}

coordinator.async_update_entity_config(
entity_id=source_entity_id, data=entry
)
coordinator.last_replaced =datetime_replaced
await coordinator.async_request_refresh()

_LOGGER.debug(
Expand All @@ -319,6 +316,24 @@ async def handle_battery_replaced(call):
str(datetime_replaced),
)

hass.bus.async_fire(
EVENT_BATTERY_REPLACED,
{
ATTR_DEVICE_ID: coordinator.device_id or "",
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id
or "",
ATTR_DEVICE_NAME: coordinator.device_name,
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
ATTR_BATTERY_TYPE: coordinator.battery_type,
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
},
)

_LOGGER.debug(
"Raised event battery replaced %s",
coordinator.device_id,
)

return

_LOGGER.error("Entity %s not configured in Battery Notes", source_entity_id)
Expand All @@ -340,11 +355,7 @@ async def handle_battery_replaced(call):
hass.data[DOMAIN][DATA].devices[entry.entry_id].coordinator
)

device_entry = {"battery_last_replaced": datetime_replaced}

coordinator.async_update_device_config(
device_id=device_id, data=device_entry
)
coordinator.last_replaced =datetime_replaced

await coordinator.async_request_refresh()

Expand All @@ -354,6 +365,24 @@ async def handle_battery_replaced(call):
str(datetime_replaced),
)

hass.bus.async_fire(
EVENT_BATTERY_REPLACED,
{
ATTR_DEVICE_ID: coordinator.device_id or "",
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id
or "",
ATTR_DEVICE_NAME: coordinator.device_name,
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
ATTR_BATTERY_TYPE: coordinator.battery_type,
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
},
)

_LOGGER.debug(
"Raised event battery replaced %s",
coordinator.device_id,
)

# Found and dealt with, exit
return

Expand All @@ -362,6 +391,7 @@ async def handle_battery_replaced(call):
device_id,
)


async def handle_battery_last_reported(call):
"""Handle the service call."""
days_last_reported = call.data.get(SERVICE_DATA_DAYS_LAST_REPORTED)
Expand Down
37 changes: 28 additions & 9 deletions custom_components/battery_notes/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import logging
from dataclasses import dataclass
from datetime import datetime

Expand Down Expand Up @@ -35,18 +36,27 @@

from . import PLATFORMS
from .const import (
ATTR_BATTERY_QUANTITY,
ATTR_BATTERY_TYPE,
ATTR_BATTERY_TYPE_AND_QUANTITY,
ATTR_DEVICE_ID,
ATTR_DEVICE_NAME,
ATTR_SOURCE_ENTITY_ID,
CONF_ENABLE_REPLACED,
CONF_SOURCE_ENTITY_ID,
DATA,
DOMAIN,
DOMAIN_CONFIG,
EVENT_BATTERY_REPLACED,
)
from .coordinator import BatteryNotesCoordinator
from .device import BatteryNotesDevice
from .entity import (
BatteryNotesEntityDescription,
)

_LOGGER = logging.getLogger(__name__)


@dataclass
class BatteryNotesButtonEntityDescription(
Expand Down Expand Up @@ -241,15 +251,24 @@ async def async_added_to_hass(self) -> None:

async def async_press(self) -> None:
"""Press the button."""
device_id = self._device_id

entry = {"battery_last_replaced": datetime.utcnow()}
self.coordinator.last_replaced = datetime.utcnow()

self.hass.bus.async_fire(
EVENT_BATTERY_REPLACED,
{
ATTR_DEVICE_ID: self.coordinator.device_id or "",
ATTR_SOURCE_ENTITY_ID: self.coordinator.source_entity_id
or "",
ATTR_DEVICE_NAME: self.coordinator.device_name,
ATTR_BATTERY_TYPE_AND_QUANTITY: self.coordinator.battery_type_and_quantity,
ATTR_BATTERY_TYPE: self.coordinator.battery_type,
ATTR_BATTERY_QUANTITY: self.coordinator.battery_quantity,
},
)

if self._source_entity_id:
self.coordinator.async_update_entity_config(
entity_id=self.coordinator.source_entity_id, data=entry
)
else:
self.coordinator.async_update_device_config(device_id=device_id, data=entry)
_LOGGER.debug(
"Raised event battery replaced %s",
self.coordinator.device_id,
)

await self.coordinator.async_request_refresh()
3 changes: 2 additions & 1 deletion 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.7"
MIN_HA_VERSION = "2024.9"

manifestfile = Path(__file__).parent / "manifest.json"
with open(file=manifestfile, encoding="UTF-8") as json_file:
Expand Down Expand Up @@ -71,6 +71,7 @@
EVENT_BATTERY_THRESHOLD = "battery_notes_battery_threshold"
EVENT_BATTERY_INCREASED = "battery_notes_battery_increased"
EVENT_BATTERY_NOT_REPORTED = "battery_notes_battery_not_reported"
EVENT_BATTERY_REPLACED = "battery_notes_battery_replaced"

ATTR_DEVICE_ID = "device_id"
ATTR_SOURCE_ENTITY_ID = "source_entity_id"
Expand Down
13 changes: 12 additions & 1 deletion custom_components/battery_notes/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def last_replaced(self) -> datetime | None:
return last_replaced_date
return None

@last_replaced.setter
def last_replaced(self, value):
"""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:
self.async_update_device_config(device_id=self.device_id, data=entry)

@property
def last_reported(self) -> datetime | None:
"""Get the last reported datetime."""
Expand All @@ -257,12 +267,13 @@ def last_reported(self) -> datetime | None:
str(entry[LAST_REPORTED]) + "+00:00"
)
return last_reported_date

return None

@last_reported.setter
def last_reported(self, value):
"""Set the last reported datetime and store it."""
entry = {"battery_last_reported": value}
entry = {LAST_REPORTED: value}

if self.source_entity_id:
self.async_update_entity_config(entity_id=self.source_entity_id, data=entry)
Expand Down
Loading

0 comments on commit 81fcac1

Please sign in to comment.