zrohacova / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
0 stars 0 forks source link

Get weather entity without hard coding #24

Open moa114 opened 1 year ago

ThushiniRanasinghe1998 commented 1 year ago

Perhaps adding these to recommendation_handler is helpful in someway to this task?

from homeassistant.helpers import entity_registry

    # Dynamically get weather entities
    weather_entities = self._get_weather_entities(hass)
    if not weather_entities:
        raise HomeAssistantError("No weather entities available")

    # Example: Use the first available weather entity
    weather_entity_id = weather_entities[0]
    #weather_entity_id = "weather.home"
    weather_state = hass.states.get(weather_entity_id)

@staticmethod
def _get_weather_entities(hass: HomeAssistant) -> list[str]:
    """Retrieve all weather entities."""
    entity_reg = entity_registry.async_get(hass)
    return [
        entity.entity_id
        for entity in entity_reg.entities.values()
        if entity.domain == "weather"
    ]