generated from ludeeus/integration_blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* An initial button * Bump ruff from 0.0.287 to 0.0.288 Bumps [ruff](https://github.com/astral-sh/ruff) from 0.0.287 to 0.0.288. - [Release notes](https://github.com/astral-sh/ruff/releases) - [Changelog](https://github.com/astral-sh/ruff/blob/main/BREAKING_CHANGES.md) - [Commits](astral-sh/ruff@v0.0.287...v0.0.288) --- updated-dependencies: - dependency-name: ruff dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Update README.md * Made the button appear * Start of multiple sensors * Remove unused const * Improving sensors * Add entity translations * Tidy up entity naming * Formatting * Update minimum requirements * Work on battery changed * Got a datetime for last changed * Work on last changed sensor * Trying things * Work on last changed time * Work on last changed * Manual merge from main refactor * WIP * WIP * Update library.json (#48) * Update library.json IKEA - TRADFRI motion sensor (E1525/E1745) - Edit to 2X CR2032 SONOFF - Temperature and humidity sensor (SNZB-02) - Edit to CR2450 IKEA - TRADFRI ON/OFF switch (E1743) - Added Configuration Saswell - Thermostatic radiator valve (SEA801-Zigbee/SEA802-Zigbee) - Added Configuration Siterwell - Radiator valve with thermostat (GS361A-H04) - Added Configuration * Update library.json Add comma --------- Co-authored-by: Andrew Jackson <andrew@codechimp.org> * Add 2 Devices (#50) * Add 2 Devices These are how two of my Aqara Zigbee devices showed up. * Update library.json Remove quantity where just 1 --------- Co-authored-by: Andrew Jackson <andrew@codechimp.org> * Update manifest.json Version bump * Add discovery screenshot * Update README.md * Update discovery screenshot * WIP * WIP * remove duplicate entries for LUMI devices (#121) * Update library.json (#122) Change HEIMAN smoke detectors, the batteries are replaceable. * Update library.json (#123) * Fix Philips Hue Dimmer switch v2 manufacturer name * Add Roborock S7 * Update library.json * Update library.json --------- Co-authored-by: Andrew Jackson <andrew@codechimp.org> * Create translation to Hungarian (#125) * Added device details (Xiaomi SRTS-A01, Sonoff TRVZB) (#127) * Added "SONOFF TRVZB" * Added "Xiaomi SRTS-A01" * Fixed formatting * Update library.json --------- Co-authored-by: Andrew Jackson <andrew@codechimp.org> * Update library.json (#128) Add Ecolink TILT-ZWAVE2.5-ECO tilt sensor * Add Drayton Wiser iTRV & RoomStat (#129) * Updated readme and additions to library * Update README.md * Add multiple devices to library (#130) * Add multiple devices to library * Update library.json --------- Co-authored-by: Andrew Jackson <andrew@codechimp.org> * Update README.md * Update validate.yml Ignore readme changes * WIP * WIP * Create battery_changed service * Change the service to use the last changed entity * WIP * WIP * WIP * Change back to device id * WIP * WIP * WIP * WIP * WIP * WIP * Button press update store * Docs * Sensor update on button & service * WIP * Tidy up storage on device remove * Check device exists on service call * Lint fixes * Update HA version to test service translation * Add placeholders for new translation requirements * Tidy up code * Add new fields to Danish translation * Remove date from service * Rename to battery replaced * Update service description * Update library * Fix lint issues & version bump --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: P-v-D <88889180+P-v-D@users.noreply.github.com> Co-authored-by: Patrick <4002194+askpatrickw@users.noreply.github.com> Co-authored-by: Wil T <wil.thieme@protonmail.com> Co-authored-by: Edouard Kleinhans <edouard@kleinhans.info> Co-authored-by: Jan Čermák <info@jan-cermak.cz> Co-authored-by: bekesizl <41523450+bekesizl@users.noreply.github.com> Co-authored-by: Janek <6506725+jkrgr0@users.noreply.github.com> Co-authored-by: Christian Allred <13487734+cgallred@users.noreply.github.com> Co-authored-by: Thomas Dietrich <Thomas@Nurzen.de>
- Loading branch information
1 parent
855d265
commit 0726151
Showing
17 changed files
with
770 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
"""Button platform for battery_notes.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_ENTITY_ID | ||
from homeassistant.core import HomeAssistant, callback, Event | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers import ( | ||
config_validation as cv, | ||
device_registry as dr, | ||
entity_registry as er, | ||
) | ||
from homeassistant.components.button import ( | ||
PLATFORM_SCHEMA, | ||
ButtonEntity, | ||
ButtonEntityDescription, | ||
) | ||
from homeassistant.helpers.entity import DeviceInfo, EntityCategory | ||
from homeassistant.helpers.event import ( | ||
async_track_entity_registry_updated_event, | ||
) | ||
|
||
from homeassistant.helpers.reload import async_setup_reload_service | ||
from homeassistant.helpers.typing import ( | ||
ConfigType, | ||
) | ||
|
||
from homeassistant.const import ( | ||
CONF_NAME, | ||
CONF_UNIQUE_ID, | ||
CONF_DEVICE_ID, | ||
) | ||
|
||
from . import PLATFORMS | ||
|
||
from .const import ( | ||
DOMAIN, | ||
DATA_COORDINATOR, | ||
) | ||
|
||
from .entity import ( | ||
BatteryNotesEntityDescription, | ||
) | ||
|
||
|
||
@dataclass | ||
class BatteryNotesButtonEntityDescription( | ||
BatteryNotesEntityDescription, | ||
ButtonEntityDescription, | ||
): | ||
"""Describes Battery Notes button entity.""" | ||
|
||
unique_id_suffix: str | ||
|
||
|
||
ENTITY_DESCRIPTIONS: tuple[BatteryNotesButtonEntityDescription, ...] = ( | ||
BatteryNotesButtonEntityDescription( | ||
unique_id_suffix="_battery_replaced_button", | ||
key="battery_replaced", | ||
translation_key="battery_replaced", | ||
icon="mdi:battery-sync", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
) | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( | ||
{vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_DEVICE_ID): cv.string} | ||
) | ||
|
||
|
||
@callback | ||
def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None: | ||
"""Add our config entry to the device.""" | ||
device_registry = dr.async_get(hass) | ||
|
||
device_id = entry.data.get(CONF_DEVICE_ID) | ||
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id) | ||
|
||
return device_id | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Initialize Battery Type config entry.""" | ||
entity_registry = er.async_get(hass) | ||
device_registry = dr.async_get(hass) | ||
|
||
device_id = config_entry.data.get(CONF_DEVICE_ID) | ||
|
||
async def async_registry_updated(event: Event) -> None: | ||
"""Handle entity registry update.""" | ||
data = event.data | ||
if data["action"] == "remove": | ||
await hass.config_entries.async_remove(config_entry.entry_id) | ||
|
||
if data["action"] != "update": | ||
return | ||
|
||
if "entity_id" in data["changes"]: | ||
# Entity_id changed, reload the config entry | ||
await hass.config_entries.async_reload(config_entry.entry_id) | ||
|
||
if device_id and "device_id" in data["changes"]: | ||
# If the tracked battery note is no longer in the device, remove our config entry | ||
# from the device | ||
if ( | ||
not (entity_entry := entity_registry.async_get(data[CONF_ENTITY_ID])) | ||
or not device_registry.async_get(device_id) | ||
or entity_entry.device_id == device_id | ||
): | ||
# No need to do any cleanup | ||
return | ||
|
||
device_registry.async_update_device( | ||
device_id, remove_config_entry_id=config_entry.entry_id | ||
) | ||
|
||
config_entry.async_on_unload( | ||
async_track_entity_registry_updated_event( | ||
hass, config_entry.entry_id, async_registry_updated | ||
) | ||
) | ||
|
||
device_id = async_add_to_device(hass, config_entry) | ||
|
||
async_add_entities( | ||
BatteryNotesButton( | ||
hass, | ||
description, | ||
f"{config_entry.entry_id}{description.unique_id_suffix}", | ||
device_id, | ||
) | ||
for description in ENTITY_DESCRIPTIONS | ||
) | ||
|
||
|
||
async def async_setup_platform( | ||
hass: HomeAssistant, | ||
config: ConfigType, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the battery type button.""" | ||
device_id: str = config[CONF_DEVICE_ID] | ||
|
||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS) | ||
|
||
async_add_entities( | ||
BatteryNotesButton( | ||
hass, | ||
description, | ||
f"{config.get(CONF_UNIQUE_ID)}{description.unique_id_suffix}", | ||
device_id, | ||
) | ||
for description in ENTITY_DESCRIPTIONS | ||
) | ||
|
||
|
||
class BatteryNotesButton(ButtonEntity): | ||
"""Represents a battery replaced button.""" | ||
|
||
_attr_should_poll = False | ||
|
||
entity_description: BatteryNotesButtonEntityDescription | ||
|
||
def __init__( | ||
self, | ||
hass: HomeAssistant, | ||
description: BatteryNotesButtonEntityDescription, | ||
unique_id: str, | ||
device_id: str, | ||
) -> None: | ||
"""Create a battery replaced button.""" | ||
device_registry = dr.async_get(hass) | ||
|
||
self.entity_description = description | ||
self._attr_unique_id = unique_id | ||
self._attr_has_entity_name = True | ||
self._device_id = device_id | ||
|
||
self._device_id = device_id | ||
if device_id and (device := device_registry.async_get(device_id)): | ||
self._attr_device_info = DeviceInfo( | ||
connections=device.connections, | ||
identifiers=device.identifiers, | ||
) | ||
|
||
async def async_added_to_hass(self) -> None: | ||
"""Handle added to Hass.""" | ||
# Update entity options | ||
registry = er.async_get(self.hass) | ||
if registry.async_get(self.entity_id) is not None: | ||
registry.async_update_entity_options( | ||
self.entity_id, | ||
DOMAIN, | ||
{"entity_id": self._attr_unique_id}, | ||
) | ||
|
||
async def update_battery_last_replaced(self): | ||
"""Handle sensor state changes.""" | ||
|
||
# device_id = self._device_id | ||
|
||
# device_entry = { | ||
# "battery_last_replaced" : datetime.utcnow() | ||
# } | ||
|
||
# coordinator = self.hass.data[DOMAIN][DATA_COORDINATOR] | ||
# coordinator.async_update_device_config(device_id = device_id, data = device_entry) | ||
|
||
self.async_write_ha_state() | ||
|
||
async def async_press(self) -> None: | ||
"""Press the button.""" | ||
device_id = self._device_id | ||
|
||
device_entry = {"battery_last_replaced": datetime.utcnow()} | ||
|
||
coordinator = self.hass.data[DOMAIN][DATA_COORDINATOR] | ||
coordinator.async_update_device_config(device_id=device_id, data=device_entry) | ||
await coordinator._async_update_data() | ||
await coordinator.async_request_refresh() |
Oops, something went wrong.