fixed bugs

This commit is contained in:
Rolf
2026-02-04 21:22:51 +01:00
parent 056c33fe8c
commit 63644dedc7
7 changed files with 15 additions and 49 deletions

View File

@@ -1,45 +1,21 @@
"""Uster Waste integration.""" """Uster Waste integration."""
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.const import Platform, SERVICE_UPDATE_ENTITY from homeassistant.const import Platform
from .const import DOMAIN from .const import DOMAIN
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR, Platform.BUTTON]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Uster Waste from a config entry.""" """Set up Uster Waste from a config entry."""
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = entry.data hass.data.setdefault(DOMAIN, {})[entry.entry_id] = entry.data
# Register manual refresh service
hass.services.async_register(
DOMAIN,
entry.data.get("name", "uster_waste"),
async_handle_manual_refresh
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_handle_manual_refresh(service):
"""Handle manual refresh service call."""
entry_id = service.data.get("entry_id")
if not entry_id or entry_id not in hass.data[DOMAIN]:
return
# Trigger update for all sensors under this entry
await hass.services.async_call(
"homeassistant", "update_entity",
{"entity_id": [f"sensor.uster_waste_{entry_id}"]},
blocking=False,
)

View File

@@ -1,6 +1,5 @@
"""Button platform for Uster Waste.""" """Button platform for Uster Waste."""
import logging import logging
from typing import Optional
from homeassistant.components.button import ButtonEntity from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@@ -18,12 +17,9 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the button.""" """Set up the button."""
config = entry.data
name = config.get("name", "Uster Waste")
entity = UsterWasteButton( entity = UsterWasteButton(
entry_id=entry.entry_id, entry_id=entry.entry_id,
name=name name=entry.data.get("name", "Uster Waste")
) )
async_add_entities([entity]) async_add_entities([entity])
@@ -45,22 +41,16 @@ class UsterWasteButton(ButtonEntity):
async def async_press(self) -> None: async def async_press(self) -> None:
"""Handle the button press (manual refresh).""" """Handle the button press (manual refresh)."""
# Trigger a manual update of the sensor # Trigger a manual update by calling the sensor's update method
hass = self.hass # The sensor will fetch fresh data from the Uster website
coordinator = None entity_registry = self.hass.data["entity_registry"]
# Find the coordinator for this entry # Find the sensor entity for this entry
if DOMAIN in hass.data: entity_id = f"sensor.uster_waste_{self._entry_id}"
for entry_id, entry_data in hass.data[DOMAIN].items():
if entry_id == self._entry_id and "coordinator" in entry_data:
coordinator = entry_data["coordinator"]
break
if coordinator: # Trigger update for the sensor
await coordinator.async_update() await self.hass.services.async_call(
# Force sensor to update "homeassistant", "update_entity",
for entity in coordinator.entities: {"entity_id": entity_id},
await entity.async_update() blocking=False,
entity.async_write_ha_state() )
else:
_LOGGER.error("Coordinator not found for manual refresh")