Olen / homeassistant-plant

Alternative Plant component of home assistant
365 stars 25 forks source link

Setup both platforms in one call #184

Closed Olen closed 4 months ago

Olen commented 4 months ago

Just a small optimization to set up both platforms with one call

ChristophCaina commented 4 months ago

Hi, I would consider to implement the following:

from homeassistant.const import (
    Platform,
    ATTR_ENTITY_PICTURE,
    ATTR_ICON,
    ATTR_NAME,
    ATTR_UNIT_OF_MEASUREMENT,
    STATE_OK,
    STATE_PROBLEM,
    STATE_UNAVAILABLE,
    STATE_UNKNOWN,
)

then

_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.SENSOR, Platform.NUMBER]

and

 await hass.config_entries.async_forward_entry_setups(entry, [PLATFORMS])
ChristophCaina commented 4 months ago

for the Unload, maybe something like this:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Unload a config entry."""
    unload_ok = await hass.config_entries.async_forward_entry_unload(entry, PLATFORMS)

    if unload_ok:
        hass.data[DOMAIN].pop(entry.entry_id)
        hass.data[DATA_UTILITY].pop(entry.entry_id)
        _LOGGER.info(hass.data[DOMAIN])
        for entry_id in list(hass.data[DOMAIN].keys()):
            if len(hass.data[DOMAIN][entry_id]) == 0:
                _LOGGER.info("Removing entry %s", entry_id)
                del hass.data[DOMAIN][entry_id]
        if len(hass.data[DOMAIN]) == 0:
            _LOGGER.info("Removing domain %s", DOMAIN)
            hass.services.async_remove(DOMAIN, SERVICE_REPLACE_SENSOR)
            del hass.data[DOMAIN]
    return unload_ok

This would result in the following, when the Unload will be tried:

  1. grafik

  2. grafik

  3. grafik

So the error message itself would be gone :)

180

ChristophCaina commented 4 months ago

I'Ve applied the changes in my own PR, if you prefer.

Olen commented 4 months ago

Replaced by #185