Add active voltage sensors to Sense integration (#41112)

pull/41503/head
Keilin Bickar 2020-10-08 12:08:37 -04:00 committed by GitHub
parent 2aee72981c
commit d8aebe145b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 4 deletions

View File

@ -2,7 +2,7 @@
"domain": "emulated_kasa",
"name": "Emulated Kasa",
"documentation": "https://www.home-assistant.io/integrations/emulated_kasa",
"requirements": ["sense_energy==0.8.0"],
"requirements": ["sense_energy==0.8.1"],
"codeowners": ["@kbickar"],
"quality_scale": "internal"
}

View File

@ -110,6 +110,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
sense_devices_data = SenseDevicesData()
try:
sense_discovered_devices = await gateway.get_discovered_device_data()
await gateway.update_realtime()
except SENSE_TIMEOUT_EXCEPTIONS as err:
raise ConfigEntryNotReady from err

View File

@ -2,7 +2,7 @@
"domain": "sense",
"name": "Sense",
"documentation": "https://www.home-assistant.io/integrations/sense",
"requirements": ["sense_energy==0.8.0"],
"requirements": ["sense_energy==0.8.1"],
"codeowners": ["@kbickar"],
"config_flow": true
}

View File

@ -6,6 +6,7 @@ from homeassistant.const import (
DEVICE_CLASS_POWER,
ENERGY_KILO_WATT_HOUR,
POWER_WATT,
VOLT,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -96,6 +97,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)
)
for i in range(len(data.active_voltage)):
devices.append(SenseVoltageSensor(data, i, sense_monitor_id))
for type_id in TRENDS_SENSOR_TYPES:
typ = TRENDS_SENSOR_TYPES[type_id]
for var in SENSOR_VARIANTS:
@ -204,6 +208,83 @@ class SenseActiveSensor(Entity):
self.async_write_ha_state()
class SenseVoltageSensor(Entity):
"""Implementation of a Sense energy voltage sensor."""
def __init__(
self,
data,
index,
sense_monitor_id,
):
"""Initialize the Sense sensor."""
line_num = index + 1
self._name = f"L{line_num} Voltage"
self._unique_id = f"{sense_monitor_id}-L{line_num}"
self._available = False
self._data = data
self._sense_monitor_id = sense_monitor_id
self._voltage_index = index
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def available(self):
"""Return the availability of the sensor."""
return self._available
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return VOLT
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
@property
def unique_id(self):
"""Return the unique id."""
return self._unique_id
@property
def should_poll(self):
"""Return the device should not poll for updates."""
return False
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{SENSE_DEVICE_UPDATE}-{self._sense_monitor_id}",
self._async_update_from_data,
)
)
@callback
def _async_update_from_data(self):
"""Update the sensor from the data. Must not do I/O."""
self._state = round(self._data.active_voltage[self._voltage_index], 1)
self._available = True
self.async_write_ha_state()
class SenseTrendsSensor(Entity):
"""Implementation of a Sense energy sensor."""

View File

@ -1987,7 +1987,7 @@ sense-hat==2.2.0
# homeassistant.components.emulated_kasa
# homeassistant.components.sense
sense_energy==0.8.0
sense_energy==0.8.1
# homeassistant.components.sentry
sentry-sdk==0.18.0

View File

@ -929,7 +929,7 @@ samsungtvws==1.4.0
# homeassistant.components.emulated_kasa
# homeassistant.components.sense
sense_energy==0.8.0
sense_energy==0.8.1
# homeassistant.components.sentry
sentry-sdk==0.18.0