fixed bugs
This commit is contained in:
@@ -1,45 +1,21 @@
|
||||
"""Uster Waste integration."""
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import Platform, SERVICE_UPDATE_ENTITY
|
||||
from homeassistant.const import Platform
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
PLATFORMS = [Platform.SENSOR, Platform.BUTTON]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Uster Waste from a config entry."""
|
||||
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)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = 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,
|
||||
)
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
Binary file not shown.
BIN
custom_components/uster_waste/__pycache__/button.cpython-313.pyc
Normal file
BIN
custom_components/uster_waste/__pycache__/button.cpython-313.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
custom_components/uster_waste/__pycache__/const.cpython-313.pyc
Normal file
BIN
custom_components/uster_waste/__pycache__/const.cpython-313.pyc
Normal file
Binary file not shown.
BIN
custom_components/uster_waste/__pycache__/sensor.cpython-313.pyc
Normal file
BIN
custom_components/uster_waste/__pycache__/sensor.cpython-313.pyc
Normal file
Binary file not shown.
@@ -1,6 +1,5 @@
|
||||
"""Button platform for Uster Waste."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from homeassistant.components.button import ButtonEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@@ -18,12 +17,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the button."""
|
||||
config = entry.data
|
||||
name = config.get("name", "Uster Waste")
|
||||
|
||||
entity = UsterWasteButton(
|
||||
entry_id=entry.entry_id,
|
||||
name=name
|
||||
name=entry.data.get("name", "Uster Waste")
|
||||
)
|
||||
async_add_entities([entity])
|
||||
|
||||
@@ -45,22 +41,16 @@ class UsterWasteButton(ButtonEntity):
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Handle the button press (manual refresh)."""
|
||||
# Trigger a manual update of the sensor
|
||||
hass = self.hass
|
||||
coordinator = None
|
||||
# Trigger a manual update by calling the sensor's update method
|
||||
# The sensor will fetch fresh data from the Uster website
|
||||
entity_registry = self.hass.data["entity_registry"]
|
||||
|
||||
# Find the coordinator for this entry
|
||||
if DOMAIN in hass.data:
|
||||
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
|
||||
# Find the sensor entity for this entry
|
||||
entity_id = f"sensor.uster_waste_{self._entry_id}"
|
||||
|
||||
if coordinator:
|
||||
await coordinator.async_update()
|
||||
# Force sensor to update
|
||||
for entity in coordinator.entities:
|
||||
await entity.async_update()
|
||||
entity.async_write_ha_state()
|
||||
else:
|
||||
_LOGGER.error("Coordinator not found for manual refresh")
|
||||
# Trigger update for the sensor
|
||||
await self.hass.services.async_call(
|
||||
"homeassistant", "update_entity",
|
||||
{"entity_id": entity_id},
|
||||
blocking=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user