Skip to content

Commit

Permalink
Use SensorEntityDescription in sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 11, 2023
1 parent d5b9b11 commit b192db6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion custom_components/integration_blueprint/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ENTITY_DESCRIPTIONS = (
BinarySensorEntityDescription(
key="integration_blueprint",
name="Integration Blueprint Sensor",
name="Integration Blueprint Binary Sensor",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
),
)
Expand Down
38 changes: 26 additions & 12 deletions custom_components/integration_blueprint/sensor.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
"""Sensor platform for integration_blueprint."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription

from .const import DEFAULT_NAME, DOMAIN, ICON, SENSOR
from .const import DOMAIN
from .coordinator import BlueprintDataUpdateCoordinator
from .entity import IntegrationBlueprintEntity


ENTITY_DESCRIPTIONS = (
SensorEntityDescription(
key="integration_blueprint",
name="Integration Sensor",
icon="mdi:format-quote-close",
),
)


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_devices([IntegrationBlueprintSensor(coordinator, entry)])
async_add_devices(
IntegrationBlueprintSensor(
coordinator=coordinator,
entity_description=entity_description,
)
for entity_description in ENTITY_DESCRIPTIONS
)


class IntegrationBlueprintSensor(IntegrationBlueprintEntity, SensorEntity):
"""integration_blueprint Sensor class."""

@property
def name(self):
"""Return the name of the sensor."""
return f"{DEFAULT_NAME}_{SENSOR}"
def __init__(
self,
coordinator: BlueprintDataUpdateCoordinator,
entity_description: SensorEntityDescription,
) -> None:
super().__init__(coordinator)
self.entity_description = entity_description

@property
def native_value(self):
"""Return the native value of the sensor."""
return self.coordinator.data.get("body")

@property
def icon(self):
"""Return the icon of the sensor."""
return ICON

0 comments on commit b192db6

Please sign in to comment.