|
| 1 | +""" |
| 2 | +Select entity platform for dahua. |
| 3 | +https://developers.home-assistant.io/docs/core/entity/select |
| 4 | +Requires HomeAssistant 2021.7.0 or greater |
| 5 | +""" |
| 6 | +from homeassistant.core import HomeAssistant |
| 7 | +from homeassistant.components.select import SelectEntity |
| 8 | +from custom_components.dahua import DahuaDataUpdateCoordinator |
| 9 | + |
| 10 | +from .const import DOMAIN |
| 11 | +from .entity import DahuaBaseEntity |
| 12 | + |
| 13 | + |
| 14 | +async def async_setup_entry(hass: HomeAssistant, entry, async_add_devices): |
| 15 | + """Setup select platform.""" |
| 16 | + coordinator: DahuaDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] |
| 17 | + |
| 18 | + devices = [] |
| 19 | + |
| 20 | + if coordinator.is_amcrest_doorbell() and coordinator.supports_security_light(): |
| 21 | + devices.append(DahuaDoorbellLightSelect(coordinator, entry)) |
| 22 | + |
| 23 | + async_add_devices(devices) |
| 24 | + |
| 25 | + |
| 26 | +class DahuaDoorbellLightSelect(DahuaBaseEntity, SelectEntity): |
| 27 | + """allows one to turn the doorbell light on/off/strobe""" |
| 28 | + |
| 29 | + def __init__(self, coordinator: DahuaDataUpdateCoordinator, config_entry): |
| 30 | + DahuaBaseEntity.__init__(self, coordinator, config_entry) |
| 31 | + SelectEntity.__init__(self) |
| 32 | + self._coordinator = coordinator |
| 33 | + self._attr_name = f"{coordinator.get_device_name()} Security Light" |
| 34 | + self._attr_unique_id = f"{coordinator.get_serial_number()}_security_light" |
| 35 | + self._attr_options = ["Off", "On", "Strobe"] |
| 36 | + |
| 37 | + @property |
| 38 | + def current_option(self) -> str: |
| 39 | + mode = self._coordinator.data.get("table.Lighting_V2[0][0][1].Mode", "") |
| 40 | + state = self._coordinator.data.get("table.Lighting_V2[0][0][1].State", "") |
| 41 | + |
| 42 | + if mode == "ForceOn" and state == "On": |
| 43 | + return "On" |
| 44 | + |
| 45 | + if mode == "ForceOn" and state == "Flicker": |
| 46 | + return "Strobe" |
| 47 | + |
| 48 | + return "Off" |
| 49 | + |
| 50 | + async def async_select_option(self, option: str) -> None: |
| 51 | + await self._coordinator.client.async_set_lighting_v2_for_amcrest_doorbells(option) |
| 52 | + await self._coordinator.async_refresh() |
| 53 | + |
| 54 | + @property |
| 55 | + def name(self): |
| 56 | + return self._attr_name |
| 57 | + |
| 58 | + @property |
| 59 | + def unique_id(self): |
| 60 | + """ https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements """ |
| 61 | + return self._attr_unique_id |
0 commit comments