|
| 1 | +"""Support for Vivint buttons.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from vivintpy.devices.alarm_panel import AlarmPanel |
| 5 | +from vivintpy.entity import UPDATE |
| 6 | + |
| 7 | +from homeassistant.components.button import ( |
| 8 | + ButtonDeviceClass, |
| 9 | + ButtonEntity, |
| 10 | + ButtonEntityDescription, |
| 11 | +) |
| 12 | +from homeassistant.config_entries import ConfigEntry |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 15 | + |
| 16 | +from .const import DOMAIN |
| 17 | +from .hub import VivintBaseEntity, VivintHub |
| 18 | + |
| 19 | +HUB_REBOOT_ENTITY = ButtonEntityDescription( |
| 20 | + key="reboot", device_class=ButtonDeviceClass.RESTART |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +async def async_setup_entry( |
| 25 | + hass: HomeAssistant, |
| 26 | + entry: ConfigEntry, |
| 27 | + async_add_entities: AddEntitiesCallback, |
| 28 | +) -> None: |
| 29 | + """Set up Vivint button platform.""" |
| 30 | + hub: VivintHub = hass.data[DOMAIN][entry.entry_id] |
| 31 | + entities = [ |
| 32 | + VivintButtonEntity( |
| 33 | + device=alarm_panel, hub=hub, entity_description=HUB_REBOOT_ENTITY |
| 34 | + ) |
| 35 | + for system in hub.account.systems |
| 36 | + if system.is_admin |
| 37 | + for alarm_panel in system.alarm_panels |
| 38 | + ] |
| 39 | + async_add_entities(entities) |
| 40 | + |
| 41 | + |
| 42 | +class VivintButtonEntity(VivintBaseEntity, ButtonEntity): |
| 43 | + """A class that describes device button entities.""" |
| 44 | + |
| 45 | + device: AlarmPanel |
| 46 | + |
| 47 | + @property |
| 48 | + def available(self) -> bool: |
| 49 | + """Return if entity is available.""" |
| 50 | + return super().available and self.device._AlarmPanel__panel.data["can_reboot"] |
| 51 | + |
| 52 | + async def async_press(self) -> None: |
| 53 | + """Handle the button press.""" |
| 54 | + await self.device.reboot() |
| 55 | + |
| 56 | + async def async_added_to_hass(self) -> None: |
| 57 | + """Set up a listener for the entity.""" |
| 58 | + await super().async_added_to_hass() |
| 59 | + self.async_on_remove( |
| 60 | + self.device._AlarmPanel__panel.on( |
| 61 | + UPDATE, lambda _: self.async_write_ha_state() |
| 62 | + ) |
| 63 | + ) |
0 commit comments