2021-02-24 11:04:38 +00:00
|
|
|
"""Setup Mullvad VPN Binary Sensors."""
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-15 19:44:41 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-02-24 11:04:38 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-03 10:32:26 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-02-24 11:04:38 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ID, CONF_NAME
|
2022-01-03 10:32:26 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-02-24 11:04:38 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
BINARY_SENSORS = (
|
|
|
|
{
|
|
|
|
CONF_ID: "mullvad_exit_ip",
|
|
|
|
CONF_NAME: "Mullvad Exit IP",
|
2021-12-15 19:44:41 +00:00
|
|
|
CONF_DEVICE_CLASS: BinarySensorDeviceClass.CONNECTIVITY,
|
2021-02-24 11:04:38 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 10:32:26 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Defer sensor setup to the shared sensor module."""
|
|
|
|
coordinator = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
async_add_entities(
|
2022-04-14 17:03:53 +00:00
|
|
|
MullvadBinarySensor(coordinator, sensor, config_entry)
|
|
|
|
for sensor in BINARY_SENSORS
|
2021-02-24 11:04:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MullvadBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|
|
|
"""Represents a Mullvad binary sensor."""
|
|
|
|
|
2022-04-14 17:03:53 +00:00
|
|
|
def __init__(self, coordinator, sensor, config_entry):
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Initialize the Mullvad binary sensor."""
|
|
|
|
super().__init__(coordinator)
|
2022-04-14 17:03:53 +00:00
|
|
|
self._sensor = sensor
|
|
|
|
self._attr_device_class = sensor[CONF_DEVICE_CLASS]
|
|
|
|
self._attr_name = sensor[CONF_NAME]
|
|
|
|
self._attr_unique_id = f"{config_entry.entry_id}_{sensor[CONF_ID]}"
|
2021-02-24 11:04:38 +00:00
|
|
|
|
|
|
|
@property
|
2021-02-24 12:43:44 +00:00
|
|
|
def is_on(self):
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Return the state for this binary sensor."""
|
2022-04-14 17:03:53 +00:00
|
|
|
return self.coordinator.data[self._sensor[CONF_ID]]
|