mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve Mullvad typing (#96545)
This commit is contained in:
parent
1e704c4abe
commit
bbc3d0d287
@ -8,7 +8,7 @@ from mullvad_api import MullvadAPI
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import update_coordinator
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
@ -23,7 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
api = await hass.async_add_executor_job(MullvadAPI)
|
||||
return api.data
|
||||
|
||||
coordinator = update_coordinator.DataUpdateCoordinator(
|
||||
coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
logging.getLogger(__name__),
|
||||
name=DOMAIN,
|
||||
|
@ -2,21 +2,24 @@
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_DEVICE_CLASS, CONF_ID, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
BINARY_SENSORS = (
|
||||
{
|
||||
CONF_ID: "mullvad_exit_ip",
|
||||
CONF_NAME: "Mullvad Exit IP",
|
||||
CONF_DEVICE_CLASS: BinarySensorDeviceClass.CONNECTIVITY,
|
||||
},
|
||||
BinarySensorEntityDescription(
|
||||
key="mullvad_exit_ip",
|
||||
name="Mullvad Exit IP",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@ -29,23 +32,26 @@ async def async_setup_entry(
|
||||
coordinator = hass.data[DOMAIN]
|
||||
|
||||
async_add_entities(
|
||||
MullvadBinarySensor(coordinator, sensor, config_entry)
|
||||
for sensor in BINARY_SENSORS
|
||||
MullvadBinarySensor(coordinator, entity_description, config_entry)
|
||||
for entity_description in BINARY_SENSORS
|
||||
)
|
||||
|
||||
|
||||
class MullvadBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||
"""Represents a Mullvad binary sensor."""
|
||||
|
||||
def __init__(self, coordinator, sensor, config_entry):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
entity_description: BinarySensorEntityDescription,
|
||||
config_entry: ConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize the Mullvad binary sensor."""
|
||||
super().__init__(coordinator)
|
||||
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]}"
|
||||
self.entity_description = entity_description
|
||||
self._attr_unique_id = f"{config_entry.entry_id}_{entity_description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
def is_on(self) -> bool:
|
||||
"""Return the state for this binary sensor."""
|
||||
return self.coordinator.data[self._sensor[CONF_ID]]
|
||||
return self.coordinator.data[self.entity_description.key]
|
||||
|
@ -2,6 +2,7 @@
|
||||
from mullvad_api import MullvadAPI, MullvadAPIError
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
@ -11,7 +12,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
async def async_step_user(self, user_input=None) -> FlowResult:
|
||||
"""Handle the initial step."""
|
||||
self._async_abort_entries_match()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user