-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
325 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"""Platform for binary sensor integration.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
import logging | ||
from typing import Final | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity import DeviceInfo, EntityCategory | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
||
from . import get_coordinator | ||
from .config_flow import API_V1, API_V2 | ||
from .const import CONF_API_VERSION, DOMAIN | ||
from .pyweatherlink import WLData | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class WLBinarySensorDescription(BinarySensorEntityDescription): | ||
"""Class describing Weatherlink binarysensor entities.""" | ||
|
||
tag: str | None = None | ||
exclude: set = () | ||
|
||
|
||
SENSOR_TYPES: Final[tuple[WLBinarySensorDescription, ...]] = ( | ||
WLBinarySensorDescription( | ||
key="TransmitterBattery", | ||
tag="trans_battery_flag", | ||
device_class=BinarySensorDeviceClass.BATTERY, | ||
translation_key="trans_battery", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
exclude=(API_V1,), | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the binary sensor platform.""" | ||
coordinator = await get_coordinator(hass, config_entry) | ||
|
||
async_add_entities( | ||
WLSensor(coordinator, hass, config_entry, description) | ||
for description in SENSOR_TYPES | ||
if config_entry.data[CONF_API_VERSION] not in description.exclude | ||
) | ||
|
||
|
||
class WLSensor(CoordinatorEntity, BinarySensorEntity): | ||
"""Representation of a Binary Sensor.""" | ||
|
||
entity_description: WLBinarySensorDescription | ||
sensor_data = WLData() | ||
|
||
def __init__( | ||
self, | ||
coordinator, | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
description: WLBinarySensorDescription, | ||
): | ||
"""Initialize the sensor.""" | ||
super().__init__(coordinator) | ||
self.hass = hass | ||
self.entry: ConfigEntry = entry | ||
self.entity_description = description | ||
self._attr_has_entity_name = True | ||
self._attr_unique_id = ( | ||
f"{self.get_unique_id_base()}-{self.entity_description.key}" | ||
) | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, self.get_unique_id_base())}, | ||
name=self.generate_name(), | ||
manufacturer="Davis", | ||
model=self.generate_model(), | ||
configuration_url="https://www.weatherlink.com/", | ||
) | ||
|
||
def get_unique_id_base(self): | ||
"""Generate base for unique_id.""" | ||
unique_base = None | ||
if self.entry.data[CONF_API_VERSION] == API_V1: | ||
unique_base = self.coordinator.data["DID"] | ||
if self.entry.data[CONF_API_VERSION] == API_V2: | ||
unique_base = self.coordinator.data["station_id_uuid"] | ||
return unique_base | ||
|
||
def generate_name(self): | ||
"""Generate device name.""" | ||
if self.entry.data[CONF_API_VERSION] == API_V1: | ||
return self.coordinator.data["station_name"] | ||
if self.entry.data[CONF_API_VERSION] == API_V2: | ||
return self.hass.data[DOMAIN][self.entry.entry_id]["station_data"][ | ||
"stations" | ||
][0]["station_name"] | ||
|
||
return "Unknown devicename" | ||
|
||
def generate_model(self): | ||
"""Generate model string.""" | ||
if self.entry.data[CONF_API_VERSION] == API_V1: | ||
return "Weatherlink - API V1" | ||
if self.entry.data[CONF_API_VERSION] == API_V2: | ||
model = self.hass.data[DOMAIN][self.entry.entry_id]["station_data"][ | ||
"stations" | ||
][0].get("product_number") | ||
return f"Weatherlink {model}" | ||
return "Weatherlink" | ||
|
||
@property | ||
def is_on(self): | ||
"""Return the state of the sensor.""" | ||
# _LOGGER.debug("Key: %s", self.entity_description.key) | ||
return self.coordinator.data.get(self.entity_description.tag) |
Oops, something went wrong.