2020-03-19 15:50:17 +00:00
|
|
|
"""Support for August sensors."""
|
|
|
|
import logging
|
|
|
|
|
2020-10-08 13:26:44 +00:00
|
|
|
from tesla_powerwall import MeterType
|
2020-04-13 19:59:50 +00:00
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-09-05 19:09:14 +00:00
|
|
|
from homeassistant.const import DEVICE_CLASS_BATTERY, DEVICE_CLASS_POWER, PERCENTAGE
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_ENERGY_EXPORTED,
|
|
|
|
ATTR_ENERGY_IMPORTED,
|
|
|
|
ATTR_FREQUENCY,
|
|
|
|
ATTR_INSTANT_AVERAGE_VOLTAGE,
|
2021-05-13 16:12:48 +00:00
|
|
|
ATTR_INSTANT_TOTAL_CURRENT,
|
2020-04-23 15:00:38 +00:00
|
|
|
ATTR_IS_ACTIVE,
|
2020-03-19 15:50:17 +00:00
|
|
|
DOMAIN,
|
2020-04-10 16:33:58 +00:00
|
|
|
ENERGY_KILO_WATT,
|
2020-03-19 15:50:17 +00:00
|
|
|
POWERWALL_API_CHARGE,
|
2020-03-31 19:55:50 +00:00
|
|
|
POWERWALL_API_DEVICE_TYPE,
|
2020-03-19 15:50:17 +00:00
|
|
|
POWERWALL_API_METERS,
|
2020-04-17 21:21:14 +00:00
|
|
|
POWERWALL_API_SERIAL_NUMBERS,
|
2020-03-31 19:55:50 +00:00
|
|
|
POWERWALL_API_SITE_INFO,
|
|
|
|
POWERWALL_API_STATUS,
|
2020-03-19 15:50:17 +00:00
|
|
|
POWERWALL_COORDINATOR,
|
|
|
|
)
|
|
|
|
from .entity import PowerWallEntity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the August sensors."""
|
|
|
|
powerwall_data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
_LOGGER.debug("Powerwall_data: %s", powerwall_data)
|
|
|
|
|
|
|
|
coordinator = powerwall_data[POWERWALL_COORDINATOR]
|
2020-03-31 19:55:50 +00:00
|
|
|
site_info = powerwall_data[POWERWALL_API_SITE_INFO]
|
|
|
|
device_type = powerwall_data[POWERWALL_API_DEVICE_TYPE]
|
|
|
|
status = powerwall_data[POWERWALL_API_STATUS]
|
2020-04-17 21:21:14 +00:00
|
|
|
powerwalls_serial_numbers = powerwall_data[POWERWALL_API_SERIAL_NUMBERS]
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
entities = []
|
2020-04-13 19:59:50 +00:00
|
|
|
for meter in MeterType:
|
2020-03-31 19:55:50 +00:00
|
|
|
entities.append(
|
2020-04-17 21:21:14 +00:00
|
|
|
PowerWallEnergySensor(
|
|
|
|
meter,
|
|
|
|
coordinator,
|
|
|
|
site_info,
|
|
|
|
status,
|
|
|
|
device_type,
|
|
|
|
powerwalls_serial_numbers,
|
|
|
|
)
|
2020-03-31 19:55:50 +00:00
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2020-04-17 21:21:14 +00:00
|
|
|
entities.append(
|
|
|
|
PowerWallChargeSensor(
|
|
|
|
coordinator, site_info, status, device_type, powerwalls_serial_numbers
|
|
|
|
)
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class PowerWallChargeSensor(PowerWallEntity, SensorEntity):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Representation of an Powerwall charge sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
|
|
|
return "Powerwall Charge"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
|
|
|
return f"{self.base_unique_id}_charge"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Get the current value in percentage."""
|
2020-10-08 13:26:44 +00:00
|
|
|
return round(self.coordinator.data[POWERWALL_API_CHARGE])
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class PowerWallEnergySensor(PowerWallEntity, SensorEntity):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Representation of an Powerwall Energy sensor."""
|
|
|
|
|
2020-04-17 21:21:14 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
meter: MeterType,
|
|
|
|
coordinator,
|
|
|
|
site_info,
|
|
|
|
status,
|
|
|
|
device_type,
|
|
|
|
powerwalls_serial_numbers,
|
|
|
|
):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-04-17 21:21:14 +00:00
|
|
|
super().__init__(
|
|
|
|
coordinator, site_info, status, device_type, powerwalls_serial_numbers
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
self._meter = meter
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
2020-04-10 16:33:58 +00:00
|
|
|
return ENERGY_KILO_WATT
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return f"Powerwall {self._meter.value.title()} Now"
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_POWER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return f"{self.base_unique_id}_{self._meter.value}_instant_power"
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2020-04-10 16:33:58 +00:00
|
|
|
"""Get the current value in kW."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return (
|
2020-08-30 12:37:11 +00:00
|
|
|
self.coordinator.data[POWERWALL_API_METERS]
|
2020-10-08 13:26:44 +00:00
|
|
|
.get_meter(self._meter)
|
2020-04-13 19:59:50 +00:00
|
|
|
.get_power(precision=3)
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Return the device specific state attributes."""
|
2020-10-08 13:26:44 +00:00
|
|
|
meter = self.coordinator.data[POWERWALL_API_METERS].get_meter(self._meter)
|
2020-03-19 15:50:17 +00:00
|
|
|
return {
|
2020-04-23 15:00:38 +00:00
|
|
|
ATTR_FREQUENCY: round(meter.frequency, 1),
|
2020-10-08 13:26:44 +00:00
|
|
|
ATTR_ENERGY_EXPORTED: meter.get_energy_exported(),
|
|
|
|
ATTR_ENERGY_IMPORTED: meter.get_energy_imported(),
|
2021-05-13 16:12:48 +00:00
|
|
|
ATTR_INSTANT_AVERAGE_VOLTAGE: round(meter.average_voltage, 1),
|
|
|
|
ATTR_INSTANT_TOTAL_CURRENT: meter.get_instant_total_current(),
|
2020-04-23 15:00:38 +00:00
|
|
|
ATTR_IS_ACTIVE: meter.is_active(),
|
2020-03-19 15:50:17 +00:00
|
|
|
}
|