|
| 1 | +"""Button platform for battery_notes.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from dataclasses import dataclass |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +import voluptuous as vol |
| 8 | + |
| 9 | +from homeassistant.config_entries import ConfigEntry |
| 10 | +from homeassistant.const import CONF_ENTITY_ID |
| 11 | +from homeassistant.core import HomeAssistant, callback, Event |
| 12 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 13 | +from homeassistant.helpers import ( |
| 14 | + config_validation as cv, |
| 15 | + device_registry as dr, |
| 16 | + entity_registry as er, |
| 17 | +) |
| 18 | +from homeassistant.components.button import ( |
| 19 | + PLATFORM_SCHEMA, |
| 20 | + ButtonEntity, |
| 21 | + ButtonEntityDescription, |
| 22 | +) |
| 23 | +from homeassistant.helpers.entity import DeviceInfo, EntityCategory |
| 24 | +from homeassistant.helpers.event import ( |
| 25 | + async_track_entity_registry_updated_event, |
| 26 | +) |
| 27 | + |
| 28 | +from homeassistant.helpers.reload import async_setup_reload_service |
| 29 | +from homeassistant.helpers.typing import ( |
| 30 | + ConfigType, |
| 31 | +) |
| 32 | + |
| 33 | +from homeassistant.const import ( |
| 34 | + CONF_NAME, |
| 35 | + CONF_UNIQUE_ID, |
| 36 | + CONF_DEVICE_ID, |
| 37 | +) |
| 38 | + |
| 39 | +from . import PLATFORMS |
| 40 | + |
| 41 | +from .const import ( |
| 42 | + DOMAIN, |
| 43 | + DATA_COORDINATOR, |
| 44 | +) |
| 45 | + |
| 46 | +from .entity import ( |
| 47 | + BatteryNotesEntityDescription, |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class BatteryNotesButtonEntityDescription( |
| 53 | + BatteryNotesEntityDescription, |
| 54 | + ButtonEntityDescription, |
| 55 | +): |
| 56 | + """Describes Battery Notes button entity.""" |
| 57 | + |
| 58 | + unique_id_suffix: str |
| 59 | + |
| 60 | + |
| 61 | +ENTITY_DESCRIPTIONS: tuple[BatteryNotesButtonEntityDescription, ...] = ( |
| 62 | + BatteryNotesButtonEntityDescription( |
| 63 | + unique_id_suffix="_battery_replaced_button", |
| 64 | + key="battery_replaced", |
| 65 | + translation_key="battery_replaced", |
| 66 | + icon="mdi:battery-sync", |
| 67 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 68 | + ), |
| 69 | +) |
| 70 | + |
| 71 | +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( |
| 72 | + {vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_DEVICE_ID): cv.string} |
| 73 | +) |
| 74 | + |
| 75 | + |
| 76 | +@callback |
| 77 | +def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None: |
| 78 | + """Add our config entry to the device.""" |
| 79 | + device_registry = dr.async_get(hass) |
| 80 | + |
| 81 | + device_id = entry.data.get(CONF_DEVICE_ID) |
| 82 | + device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id) |
| 83 | + |
| 84 | + return device_id |
| 85 | + |
| 86 | + |
| 87 | +async def async_setup_entry( |
| 88 | + hass: HomeAssistant, |
| 89 | + config_entry: ConfigEntry, |
| 90 | + async_add_entities: AddEntitiesCallback, |
| 91 | +) -> None: |
| 92 | + """Initialize Battery Type config entry.""" |
| 93 | + entity_registry = er.async_get(hass) |
| 94 | + device_registry = dr.async_get(hass) |
| 95 | + |
| 96 | + device_id = config_entry.data.get(CONF_DEVICE_ID) |
| 97 | + |
| 98 | + async def async_registry_updated(event: Event) -> None: |
| 99 | + """Handle entity registry update.""" |
| 100 | + data = event.data |
| 101 | + if data["action"] == "remove": |
| 102 | + await hass.config_entries.async_remove(config_entry.entry_id) |
| 103 | + |
| 104 | + if data["action"] != "update": |
| 105 | + return |
| 106 | + |
| 107 | + if "entity_id" in data["changes"]: |
| 108 | + # Entity_id changed, reload the config entry |
| 109 | + await hass.config_entries.async_reload(config_entry.entry_id) |
| 110 | + |
| 111 | + if device_id and "device_id" in data["changes"]: |
| 112 | + # If the tracked battery note is no longer in the device, remove our config entry |
| 113 | + # from the device |
| 114 | + if ( |
| 115 | + not (entity_entry := entity_registry.async_get(data[CONF_ENTITY_ID])) |
| 116 | + or not device_registry.async_get(device_id) |
| 117 | + or entity_entry.device_id == device_id |
| 118 | + ): |
| 119 | + # No need to do any cleanup |
| 120 | + return |
| 121 | + |
| 122 | + device_registry.async_update_device( |
| 123 | + device_id, remove_config_entry_id=config_entry.entry_id |
| 124 | + ) |
| 125 | + |
| 126 | + config_entry.async_on_unload( |
| 127 | + async_track_entity_registry_updated_event( |
| 128 | + hass, config_entry.entry_id, async_registry_updated |
| 129 | + ) |
| 130 | + ) |
| 131 | + |
| 132 | + device_id = async_add_to_device(hass, config_entry) |
| 133 | + |
| 134 | + async_add_entities( |
| 135 | + BatteryNotesButton( |
| 136 | + hass, |
| 137 | + description, |
| 138 | + f"{config_entry.entry_id}{description.unique_id_suffix}", |
| 139 | + device_id, |
| 140 | + ) |
| 141 | + for description in ENTITY_DESCRIPTIONS |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +async def async_setup_platform( |
| 146 | + hass: HomeAssistant, |
| 147 | + config: ConfigType, |
| 148 | + async_add_entities: AddEntitiesCallback, |
| 149 | +) -> None: |
| 150 | + """Set up the battery type button.""" |
| 151 | + device_id: str = config[CONF_DEVICE_ID] |
| 152 | + |
| 153 | + await async_setup_reload_service(hass, DOMAIN, PLATFORMS) |
| 154 | + |
| 155 | + async_add_entities( |
| 156 | + BatteryNotesButton( |
| 157 | + hass, |
| 158 | + description, |
| 159 | + f"{config.get(CONF_UNIQUE_ID)}{description.unique_id_suffix}", |
| 160 | + device_id, |
| 161 | + ) |
| 162 | + for description in ENTITY_DESCRIPTIONS |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +class BatteryNotesButton(ButtonEntity): |
| 167 | + """Represents a battery replaced button.""" |
| 168 | + |
| 169 | + _attr_should_poll = False |
| 170 | + |
| 171 | + entity_description: BatteryNotesButtonEntityDescription |
| 172 | + |
| 173 | + def __init__( |
| 174 | + self, |
| 175 | + hass: HomeAssistant, |
| 176 | + description: BatteryNotesButtonEntityDescription, |
| 177 | + unique_id: str, |
| 178 | + device_id: str, |
| 179 | + ) -> None: |
| 180 | + """Create a battery replaced button.""" |
| 181 | + device_registry = dr.async_get(hass) |
| 182 | + |
| 183 | + self.entity_description = description |
| 184 | + self._attr_unique_id = unique_id |
| 185 | + self._attr_has_entity_name = True |
| 186 | + self._device_id = device_id |
| 187 | + |
| 188 | + self._device_id = device_id |
| 189 | + if device_id and (device := device_registry.async_get(device_id)): |
| 190 | + self._attr_device_info = DeviceInfo( |
| 191 | + connections=device.connections, |
| 192 | + identifiers=device.identifiers, |
| 193 | + ) |
| 194 | + |
| 195 | + async def async_added_to_hass(self) -> None: |
| 196 | + """Handle added to Hass.""" |
| 197 | + # Update entity options |
| 198 | + registry = er.async_get(self.hass) |
| 199 | + if registry.async_get(self.entity_id) is not None: |
| 200 | + registry.async_update_entity_options( |
| 201 | + self.entity_id, |
| 202 | + DOMAIN, |
| 203 | + {"entity_id": self._attr_unique_id}, |
| 204 | + ) |
| 205 | + |
| 206 | + async def update_battery_last_replaced(self): |
| 207 | + """Handle sensor state changes.""" |
| 208 | + |
| 209 | + # device_id = self._device_id |
| 210 | + |
| 211 | + # device_entry = { |
| 212 | + # "battery_last_replaced" : datetime.utcnow() |
| 213 | + # } |
| 214 | + |
| 215 | + # coordinator = self.hass.data[DOMAIN][DATA_COORDINATOR] |
| 216 | + # coordinator.async_update_device_config(device_id = device_id, data = device_entry) |
| 217 | + |
| 218 | + self.async_write_ha_state() |
| 219 | + |
| 220 | + async def async_press(self) -> None: |
| 221 | + """Press the button.""" |
| 222 | + device_id = self._device_id |
| 223 | + |
| 224 | + device_entry = {"battery_last_replaced": datetime.utcnow()} |
| 225 | + |
| 226 | + coordinator = self.hass.data[DOMAIN][DATA_COORDINATOR] |
| 227 | + coordinator.async_update_device_config(device_id=device_id, data=device_entry) |
| 228 | + await coordinator._async_update_data() |
| 229 | + await coordinator.async_request_refresh() |
0 commit comments