Skip to content

Commit

Permalink
fix: add entities under service (#59)
Browse files Browse the repository at this point in the history
* fix: add entities under service

* fix typo

* update binary sensor properties
  • Loading branch information
firstof9 authored Apr 4, 2022
1 parent c941615 commit ecf8c68
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
48 changes: 37 additions & 11 deletions custom_components/openei/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Binary sensor platform for OpenEI."""
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify

Expand All @@ -12,28 +16,50 @@ async def async_setup_entry(hass, entry, async_add_devices):

binary_sensors = []
for binary_sensor in BINARY_SENSORS:
binary_sensors.append(OpenEIBinarySensor(coordinator, entry, binary_sensor))
binary_sensors.append(OpenEIBinarySensor(hass, BINARY_SENSORS[binary_sensor], entry, coordinator))

async_add_devices(binary_sensors, False)


class OpenEIBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""integration_blueprint binary_sensor class."""

def __init__(self, coordinator, entry, sensor_type) -> None:
def __init__(
self,
hass: HomeAssistant,
sensor_description: BinarySensorEntityDescription,
entry: ConfigEntry,
coordinator: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._name = sensor_type
self._name = sensor_description.name
self._key = sensor_description.key
self._unique_id = entry.entry_id
self._icon = sensor_description.icon
self._config = entry
self.coordinator = coordinator
self._attr_is_on = coordinator.data.get(sensor_type)
self._attr_is_on = coordinator.data.get(self._key)

self._attr_name = f"{slugify(self._config.title)}_{self._name}"
self._attr_unique_id = f"{self._key}_{self._unique_id}"

@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success

@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return f"{self._name}_{self._config.entry_id}"
def icon(self) -> str:
"""Return the icon."""
return self._icon

@property
def name(self):
"""Return the name of the binary_sensor."""
return f"{slugify(self._config.title)}_{BINARY_SENSORS[self._name][0]}"
def device_info(self) -> DeviceInfo:
"""Return device registry information."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, self._config.entry_id)},
manufacturer="OpenEI",
name="OpenEI",
)
22 changes: 18 additions & 4 deletions custom_components/openei/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Constants for integration_blueprint."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.components.sensor import (
SensorEntityDescription,
)
from homeassistant.helpers.entity import EntityCategory

from typing import Final

Expand Down Expand Up @@ -52,6 +58,7 @@
key="distributed_generation",
name="Distributed Generation",
icon="mdi:gauge",
entity_category=EntityCategory.DIAGNOSTIC,
),
"rate_name": SensorEntityDescription(
key="rate_name",
Expand All @@ -62,6 +69,7 @@
key="all_rates",
name="All Listed Rates",
icon="mdi:format-list-bulleted",
entity_category=EntityCategory.DIAGNOSTIC,
),
"monthly_tier_rate": SensorEntityDescription(
key="monthly_tier_rate",
Expand All @@ -72,9 +80,15 @@
key="mincharge",
name="Minimum Charge",
icon="mdi:cash-multiple",
entity_category=EntityCategory.DIAGNOSTIC,
),
}

BINARY_SENSORS = {
"approval": ["Approval", "mdi:check", None, None],
}
BINARY_SENSORS: Final[dict[str, BinarySensorEntityDescription]] = {
"approval": BinarySensorEntityDescription(
name="Approval",
key="approval",
icon="mdi:check",
entity_category=EntityCategory.DIAGNOSTIC,
),
}
14 changes: 13 additions & 1 deletion custom_components/openei/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify

Expand Down Expand Up @@ -75,4 +77,14 @@ def extra_state_attributes(self) -> Optional[dict]:
@property
def icon(self) -> str:
"""Return the icon."""
return self._icon
return self._icon

@property
def device_info(self) -> DeviceInfo:
"""Return device registry information."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, self._config.entry_id)},
manufacturer="OpenEI",
name="OpenEI",
)

0 comments on commit ecf8c68

Please sign in to comment.