|
| 1 | +from typing import Any |
| 2 | +from homeassistant.components.cover import ( |
| 3 | + CoverDeviceClass, |
| 4 | + CoverEntity, |
| 5 | + CoverEntityFeature, |
| 6 | +) |
| 7 | +from homeassistant.config_entries import ConfigEntry |
| 8 | +from homeassistant.const import EntityCategory |
| 9 | +from homeassistant.core import HomeAssistant |
| 10 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 11 | + |
| 12 | +from .base import TeslaFiEntity, TeslaFiCoverEntityDescription |
| 13 | +from .const import DOMAIN, LOGGER |
| 14 | +from .coordinator import TeslaFiCoordinator |
| 15 | +from .errors import TeslaFiApiError |
| 16 | +from .util import _convert_to_bool |
| 17 | + |
| 18 | +COVERS = [ |
| 19 | + TeslaFiCoverEntityDescription( |
| 20 | + key="charge_port_door_open", |
| 21 | + name="Charge Port Door", |
| 22 | + device_class=CoverDeviceClass.DOOR, |
| 23 | + icon="mdi:ev-plug-tesla", |
| 24 | + value=lambda d, h: d.get("charge_port_door_open", False), |
| 25 | + available=lambda u, d, h: u and d.car_state != "driving", |
| 26 | + cmd=lambda c, v: c.execute_command( |
| 27 | + "charge_port_door_open" if v else "charge_port_door_close" |
| 28 | + ), |
| 29 | + ) |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +class TeslaFiCoverEntity( |
| 34 | + TeslaFiEntity[TeslaFiCoverEntityDescription], |
| 35 | + CoverEntity, |
| 36 | +): |
| 37 | + """TeslaFi Cover Entity""" |
| 38 | + |
| 39 | + _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE |
| 40 | + _attr_is_closed = False |
| 41 | + _attr_is_closing = False |
| 42 | + _attr_is_opening = False |
| 43 | + |
| 44 | + def _handle_coordinator_update(self) -> None: |
| 45 | + self._attr_is_closed = self._get_value() == False |
| 46 | + self._attr_is_opening = False |
| 47 | + self._attr_is_closing = False |
| 48 | + return super()._handle_coordinator_update() |
| 49 | + |
| 50 | + async def async_close_cover(self, **kwargs: Any): |
| 51 | + if self.coordinator.data.is_plugged_in: |
| 52 | + LOGGER.warning("Cannot close charge port door while plugged in!") |
| 53 | + |
| 54 | + try: |
| 55 | + self._attr_is_opening = False |
| 56 | + await self.entity_description.cmd(self.coordinator, False) |
| 57 | + self._attr_is_closed = True |
| 58 | + except TeslaFiApiError as e: |
| 59 | + if "already closed" not in str(e): |
| 60 | + raise e |
| 61 | + self._attr_is_closed = True |
| 62 | + return self.async_write_ha_state() |
| 63 | + |
| 64 | + async def async_open_cover(self, **kwargs: Any) -> None: |
| 65 | + if self.coordinator.data.shift_state != "park": |
| 66 | + LOGGER.warning("Cannot open charge port door while driving!") |
| 67 | + try: |
| 68 | + self._attr_is_closing = False |
| 69 | + await self.entity_description.cmd(self.coordinator, True) |
| 70 | + self._attr_is_closed = False |
| 71 | + except TeslaFiApiError as e: |
| 72 | + if "already open" not in str(e): |
| 73 | + raise e |
| 74 | + self._attr_is_closed = False |
| 75 | + return self.async_write_ha_state() |
| 76 | + |
| 77 | + |
| 78 | +async def async_setup_entry( |
| 79 | + hass: HomeAssistant, |
| 80 | + config_entry: ConfigEntry, |
| 81 | + async_add_entities: AddEntitiesCallback, |
| 82 | +) -> None: |
| 83 | + """Set up from config entry""" |
| 84 | + coordinator: TeslaFiCoordinator |
| 85 | + coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"] |
| 86 | + entities: list[TeslaFiCoverEntity] = [] |
| 87 | + entities.extend( |
| 88 | + [TeslaFiCoverEntity(coordinator, description) for description in COVERS] |
| 89 | + ) |
| 90 | + async_add_entities(entities) |
0 commit comments