Add configuration_url to Sense (#57814)

pull/57832/head
J. Nick Koston 2021-10-15 18:53:27 -10:00 committed by GitHub
parent 1cf7634942
commit 8405331204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 30 deletions

View File

@ -92,6 +92,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
update_method=gateway.update_trend_data,
update_interval=timedelta(seconds=300),
)
# Start out as unavailable so we do not report 0 data
# until the update happens
trends_coordinator.last_update_success = False
# This can take longer than 60s and we already know
# sense is online since get_discovered_device_data was

View File

@ -15,6 +15,7 @@ from homeassistant.const import (
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
ACTIVE_NAME,
@ -85,11 +86,10 @@ def sense_to_mdi(sense_icon):
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Sense sensor."""
data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DATA]
sense_devices_data = hass.data[DOMAIN][config_entry.entry_id][SENSE_DEVICES_DATA]
trends_coordinator = hass.data[DOMAIN][config_entry.entry_id][
SENSE_TRENDS_COORDINATOR
]
base_data = hass.data[DOMAIN][config_entry.entry_id]
data = base_data[SENSE_DATA]
sense_devices_data = base_data[SENSE_DEVICES_DATA]
trends_coordinator = base_data[SENSE_TRENDS_COORDINATOR]
# Request only in case it takes longer
# than 60s
@ -141,6 +141,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
variant_name,
trends_coordinator,
unique_id,
sense_monitor_id,
)
)
@ -245,7 +246,7 @@ class SenseVoltageSensor(SensorEntity):
self.async_write_ha_state()
class SenseTrendsSensor(SensorEntity):
class SenseTrendsSensor(CoordinatorEntity, SensorEntity):
"""Implementation of a Sense energy sensor."""
_attr_device_class = DEVICE_CLASS_ENERGY
@ -264,49 +265,34 @@ class SenseTrendsSensor(SensorEntity):
variant_name,
trends_coordinator,
unique_id,
sense_monitor_id,
):
"""Initialize the Sense sensor."""
super().__init__(trends_coordinator)
self._attr_name = f"{name} {variant_name}"
self._attr_unique_id = unique_id
self._data = data
self._sensor_type = sensor_type
self._coordinator = trends_coordinator
self._variant_id = variant_id
self._had_any_update = False
if variant_id in [PRODUCTION_PCT_ID, SOLAR_POWERED_ID]:
self._attr_native_unit_of_measurement = PERCENTAGE
self._attr_entity_registry_enabled_default = False
self._attr_state_class = None
self._attr_device_class = None
self._attr_device_info = {
"name": f"Sense {sense_monitor_id}",
"identifiers": {(DOMAIN, sense_monitor_id)},
"model": "Sense",
"manufacturer": "Sense Labs, Inc.",
"configuration_url": "https://home.sense.com",
}
@property
def native_value(self):
"""Return the state of the sensor."""
return round(self._data.get_trend(self._sensor_type, self._variant_id), 1)
@property
def available(self):
"""Return if entity is available."""
return self._had_any_update and self._coordinator.last_update_success
@callback
def _async_update(self):
"""Track if we had an update so we do not report zero data."""
self._had_any_update = True
self.async_write_ha_state()
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(self._coordinator.async_add_listener(self._async_update))
class SenseEnergyDevice(SensorEntity):
"""Implementation of a Sense energy device."""