|
| 1 | +"""Cover group.""" |
| 2 | + |
| 3 | +from functools import cached_property |
| 4 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 5 | +from homeassistant.components.group.cover import CoverGroup |
| 6 | +from homeassistant.helpers.device_registry import DeviceInfo |
| 7 | +from homeassistant.components.cover import ( |
| 8 | + CoverDeviceClass, |
| 9 | + DEVICE_CLASSES as COVER_DEVICE_CLASSES, |
| 10 | +) |
| 11 | + |
| 12 | +from custom_components.auto_areas.auto_area import AutoArea |
| 13 | +from custom_components.auto_areas.const import ( |
| 14 | + COVER_GROUP_ENTITY_PREFIX, |
| 15 | + COVER_GROUP_PREFIX, |
| 16 | + DOMAIN, |
| 17 | + LOGGER, |
| 18 | + NAME, |
| 19 | + VERSION |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback): |
| 24 | + """Set up the cover platform.""" |
| 25 | + auto_area: AutoArea = hass.data[DOMAIN][entry.entry_id] |
| 26 | + |
| 27 | + cover_entity_ids: list[str] = auto_area.get_area_entity_ids( |
| 28 | + COVER_DEVICE_CLASSES |
| 29 | + ) |
| 30 | + if not cover_entity_ids: |
| 31 | + LOGGER.info( |
| 32 | + "%s: No covers found in area. Not creating cover group.", |
| 33 | + auto_area.area_name, |
| 34 | + ) |
| 35 | + else: |
| 36 | + async_add_entities([AutoCoverGroup( |
| 37 | + hass, |
| 38 | + auto_area, |
| 39 | + entity_ids=cover_entity_ids |
| 40 | + )]) |
| 41 | + |
| 42 | + |
| 43 | +class AutoCoverGroup(CoverGroup): |
| 44 | + """Cover group with area covers.""" |
| 45 | + |
| 46 | + def __init__(self, hass, auto_area: AutoArea, entity_ids: list[str]) -> None: |
| 47 | + """Initialize cover group.""" |
| 48 | + self.hass = hass |
| 49 | + self.auto_area = auto_area |
| 50 | + self._device_class = CoverDeviceClass.BLIND |
| 51 | + self._name_prefix = COVER_GROUP_PREFIX |
| 52 | + self._prefix = COVER_GROUP_ENTITY_PREFIX |
| 53 | + self.entity_ids: list[str] = entity_ids |
| 54 | + |
| 55 | + CoverGroup.__init__( |
| 56 | + self, |
| 57 | + entities=self.entity_ids, |
| 58 | + name=None, |
| 59 | + unique_id=self._attr_unique_id, |
| 60 | + ) |
| 61 | + |
| 62 | + LOGGER.debug("Initialized cover group %s", self.entity_ids) |
| 63 | + |
| 64 | + @cached_property |
| 65 | + def name(self): |
| 66 | + """Name of this entity.""" |
| 67 | + return f"{self._name_prefix}{self.auto_area.area_name}" |
| 68 | + |
| 69 | + @cached_property |
| 70 | + def device_info(self) -> DeviceInfo: |
| 71 | + """Information about this device.""" |
| 72 | + return { |
| 73 | + "identifiers": {(DOMAIN, self.auto_area.config_entry.entry_id)}, |
| 74 | + "name": NAME, |
| 75 | + "model": VERSION, |
| 76 | + "manufacturer": NAME, |
| 77 | + "suggested_area": self.auto_area.area_name, |
| 78 | + } |
| 79 | + |
| 80 | + @cached_property |
| 81 | + def unique_id(self) -> str | None: |
| 82 | + """Return a unique ID.""" |
| 83 | + return f"{self.auto_area.config_entry.entry_id}_cover_group" |
0 commit comments