Rework goalzero for EntityDescription (#54786)
* Rework goalzero for EntityDescription * changes * fix * lint
This commit is contained in:
parent
d3f7312834
commit
e11ffbcdaf
6 changed files with 252 additions and 232 deletions
|
@ -1,14 +1,51 @@
|
|||
"""Support for Goal Zero Yeti Sensors."""
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON, ATTR_NAME, CONF_NAME
|
||||
from __future__ import annotations
|
||||
|
||||
from . import YetiEntity
|
||||
from .const import BINARY_SENSOR_DICT, DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_BATTERY_CHARGING,
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
DEVICE_CLASS_POWER,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import Yeti, YetiEntity
|
||||
from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||
BinarySensorEntityDescription(
|
||||
key="backlight",
|
||||
name="Backlight",
|
||||
icon="mdi:clock-digital",
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="app_online",
|
||||
name="App Online",
|
||||
device_class=DEVICE_CLASS_CONNECTIVITY,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="isCharging",
|
||||
name="Charging",
|
||||
device_class=DEVICE_CLASS_BATTERY_CHARGING,
|
||||
),
|
||||
BinarySensorEntityDescription(
|
||||
key="inputDetected",
|
||||
name="Input Detected",
|
||||
device_class=DEVICE_CLASS_POWER,
|
||||
),
|
||||
)
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the Goal Zero Yeti sensor."""
|
||||
name = entry.data[CONF_NAME]
|
||||
goalzero_data = hass.data[DOMAIN][entry.entry_id]
|
||||
|
@ -17,10 +54,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
goalzero_data[DATA_KEY_API],
|
||||
goalzero_data[DATA_KEY_COORDINATOR],
|
||||
name,
|
||||
sensor_name,
|
||||
description,
|
||||
entry.entry_id,
|
||||
)
|
||||
for sensor_name in BINARY_SENSOR_DICT
|
||||
for description in BINARY_SENSOR_TYPES
|
||||
)
|
||||
|
||||
|
||||
|
@ -29,26 +66,19 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
api,
|
||||
coordinator,
|
||||
name,
|
||||
sensor_name,
|
||||
server_unique_id,
|
||||
):
|
||||
api: Yeti,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
name: str,
|
||||
description: BinarySensorEntityDescription,
|
||||
server_unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize a Goal Zero Yeti sensor."""
|
||||
super().__init__(api, coordinator, name, server_unique_id)
|
||||
|
||||
self._condition = sensor_name
|
||||
self._attr_device_class = BINARY_SENSOR_DICT[sensor_name].get(ATTR_DEVICE_CLASS)
|
||||
self._attr_icon = BINARY_SENSOR_DICT[sensor_name].get(ATTR_ICON)
|
||||
self._attr_name = f"{name} {BINARY_SENSOR_DICT[sensor_name].get(ATTR_NAME)}"
|
||||
self._attr_unique_id = (
|
||||
f"{server_unique_id}/{BINARY_SENSOR_DICT[sensor_name].get(ATTR_NAME)}"
|
||||
)
|
||||
self.entity_description = description
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
self._attr_unique_id = f"{server_unique_id}/{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return if the service is on."""
|
||||
if self.api.data:
|
||||
return self.api.data[self._condition] == 1
|
||||
return False
|
||||
return self.api.data.get(self.entity_description.key) == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue