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,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ID, CONF_NAME
|
|
|
|
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
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Defer sensor setup to the shared sensor module."""
|
|
|
|
coordinator = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
MullvadBinarySensor(coordinator, sensor) for sensor in BINARY_SENSORS
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MullvadBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
|
|
|
"""Represents a Mullvad binary sensor."""
|
|
|
|
|
2021-03-02 08:02:04 +00:00
|
|
|
def __init__(self, coordinator, sensor):
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Initialize the Mullvad binary sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self.id = sensor[CONF_ID]
|
|
|
|
self._name = sensor[CONF_NAME]
|
|
|
|
self._device_class = sensor[CONF_DEVICE_CLASS]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class for this binary sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name for this binary sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@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."""
|
|
|
|
return self.coordinator.data[self.id]
|