core/homeassistant/components/mullvad/binary_sensor.py

58 lines
1.7 KiB
Python
Raw Normal View History

"""Setup Mullvad VPN Binary Sensors."""
from homeassistant.components.binary_sensor import (
2021-12-15 19:44:41 +00:00
BinarySensorDeviceClass,
BinarySensorEntity,
2023-07-14 19:24:41 +00:00
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2023-07-14 19:24:41 +00:00
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .const import DOMAIN
BINARY_SENSORS = (
2023-07-14 19:24:41 +00:00
BinarySensorEntityDescription(
key="mullvad_exit_ip",
name="Mullvad Exit IP",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Defer sensor setup to the shared sensor module."""
coordinator = hass.data[DOMAIN]
async_add_entities(
2023-07-14 19:24:41 +00:00
MullvadBinarySensor(coordinator, entity_description, config_entry)
for entity_description in BINARY_SENSORS
)
class MullvadBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Represents a Mullvad binary sensor."""
2023-07-14 19:24:41 +00:00
def __init__(
self,
coordinator: DataUpdateCoordinator,
entity_description: BinarySensorEntityDescription,
config_entry: ConfigEntry,
) -> None:
"""Initialize the Mullvad binary sensor."""
super().__init__(coordinator)
2023-07-14 19:24:41 +00:00
self.entity_description = entity_description
self._attr_unique_id = f"{config_entry.entry_id}_{entity_description.key}"
@property
2023-07-14 19:24:41 +00:00
def is_on(self) -> bool:
"""Return the state for this binary sensor."""
2023-07-14 19:24:41 +00:00
return self.coordinator.data[self.entity_description.key]