Skip to content

Commit

Permalink
prefix sensors with utility name (#25)
Browse files Browse the repository at this point in the history
* prefix sensors with utility name

fixes #22

* add missing default
  • Loading branch information
firstof9 authored Sep 8, 2021
1 parent bbff8fd commit e2caf69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 11 additions & 1 deletion custom_components/openei/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CONF_API_KEY,
CONF_PLAN,
CONF_RADIUS,
CONF_SENSOR,
DOMAIN,
PLATFORMS,
SENSOR_TYPES,
Expand All @@ -37,6 +38,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)

updated_config = entry.data.copy()

if entry.data.get(CONF_SENSOR) == "(none)":
updated_config[CONF_SENSOR] = None

if updated_config != entry.data:
hass.config_entries.async_update_entry(entry, data=updated_config)

coordinator = OpenEIDataUpdateCoordinator(hass, config=entry)
await coordinator.async_refresh()

Expand Down Expand Up @@ -109,7 +118,8 @@ def get_sensors(hass, config):
lon = hass.config.longitude
plan = config.data.get(CONF_PLAN)
radius = config.data.get(CONF_RADIUS)
rate = openeihttp.Rates(api, lat, lon, plan, radius)
readings = config.data.get(CONF_SENSOR)
rate = openeihttp.Rates(api, lat, lon, plan, radius, readings)
rate.update()
data = {}

Expand Down
5 changes: 3 additions & 2 deletions custom_components/openei/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Binary sensor platform for OpenEI."""
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify

from .const import BINARY_SENSORS, DOMAIN

Expand Down Expand Up @@ -30,9 +31,9 @@ def __init__(self, coordinator, entry, sensor_type) -> None:
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return self._config.entry_id
return f"{self._name}_{self._config.entry_id}"

@property
def name(self):
"""Return the name of the binary_sensor."""
return f"{BINARY_SENSORS[self._name][0]}"
return f"{slugify(self._config.title)}_{BINARY_SENSORS[self._name][0]}"
6 changes: 3 additions & 3 deletions custom_components/openei/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ def _get_default(key: str, fallback_default: Any = None) -> None:
CONF_API_KEY, default=_get_default(CONF_API_KEY, "")
): cv.string,
vol.Optional(CONF_RADIUS, default=_get_default(CONF_RADIUS, "")): cv.string,
vol.Optional(CONF_SENSOR, default=_get_default(CONF_SENSOR, "")): vol.In(
_get_entities(hass, SENSORS_DOMAIN, "energy")
),
},
)

Expand Down Expand Up @@ -244,6 +241,9 @@ def _get_default(key: str, fallback_default: Any = None) -> None:
vol.Required(CONF_PLAN, default=_get_default(CONF_PLAN, "")): vol.In(
plan_list
),
vol.Optional(
CONF_SENSOR, default=_get_default(CONF_SENSOR, "(none)")
): vol.In(_get_entities(hass, SENSORS_DOMAIN, "energy", ["(none)"])),
},
)

Expand Down
12 changes: 7 additions & 5 deletions custom_components/openei/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify


from .const import ATTRIBUTION, DOMAIN, SENSOR_TYPES


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
unique_id = entry.entry_id

sensors = []
for sensor in SENSOR_TYPES:
sensors.append(OpenEISensor(hass, sensor, unique_id, coordinator))
sensors.append(OpenEISensor(hass, sensor, entry, coordinator))

async_add_devices(sensors, False)


class OpenEISensor(CoordinatorEntity, SensorEntity):
"""OpenEI Sensor class."""

def __init__(self, hass, sensor_type, unique_id, coordinator) -> None:
def __init__(self, hass, sensor_type, entry, coordinator) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.hass = hass
self._name = sensor_type
self._unique_id = unique_id
self._unique_id = entry.entry_id
self._config = entry
self.coordinator = coordinator
self._attr_native_unit_of_measurement = (
f"{self.hass.config.currency}/kWh" if self._name == "current_rate" else None
Expand All @@ -44,7 +46,7 @@ def unique_id(self) -> str:
@property
def name(self):
"""Return the name of the sensor."""
return f"{SENSOR_TYPES[self._name][0]}"
return f"{self._config.title}_{SENSOR_TYPES[self._name][0]}"

@property
def icon(self) -> str:
Expand Down

0 comments on commit e2caf69

Please sign in to comment.