2020-04-20 01:50:42 +00:00
|
|
|
"""Support for powerwall binary sensors."""
|
2020-03-19 15:50:17 +00:00
|
|
|
import logging
|
|
|
|
|
2020-04-13 19:59:50 +00:00
|
|
|
from tesla_powerwall import GridStatus
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2020-04-20 01:50:42 +00:00
|
|
|
DEVICE_CLASS_BATTERY_CHARGING,
|
2020-03-19 15:50:17 +00:00
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
BinarySensorDevice,
|
|
|
|
)
|
|
|
|
from homeassistant.const import DEVICE_CLASS_POWER
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_GRID_CODE,
|
|
|
|
ATTR_NOMINAL_SYSTEM_POWER,
|
|
|
|
ATTR_REGION,
|
2020-04-20 01:50:42 +00:00
|
|
|
CHARGING_MARGIN_OF_ERROR,
|
2020-03-19 15:50:17 +00:00
|
|
|
DOMAIN,
|
2020-03-31 19:55:50 +00:00
|
|
|
POWERWALL_API_DEVICE_TYPE,
|
2020-03-19 15:50:17 +00:00
|
|
|
POWERWALL_API_GRID_STATUS,
|
2020-04-20 01:50:42 +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,
|
2020-03-19 15:50:17 +00:00
|
|
|
POWERWALL_API_SITEMASTER,
|
2020-03-31 19:55:50 +00:00
|
|
|
POWERWALL_API_STATUS,
|
2020-04-20 01:50:42 +00:00
|
|
|
POWERWALL_BATTERY_METER,
|
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]
|
|
|
|
|
|
|
|
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 = []
|
|
|
|
for sensor_class in (
|
|
|
|
PowerWallRunningSensor,
|
|
|
|
PowerWallGridStatusSensor,
|
|
|
|
PowerWallConnectedSensor,
|
2020-04-20 01:50:42 +00:00
|
|
|
PowerWallChargingStatusSensor,
|
2020-03-19 15:50:17 +00:00
|
|
|
):
|
2020-04-17 21:21:14 +00:00
|
|
|
entities.append(
|
|
|
|
sensor_class(
|
|
|
|
coordinator, site_info, status, device_type, powerwalls_serial_numbers
|
|
|
|
)
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
|
|
|
class PowerWallRunningSensor(PowerWallEntity, BinarySensorDevice):
|
|
|
|
"""Representation of an Powerwall running sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
|
|
|
return "Powerwall Status"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_POWER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
|
|
|
return f"{self.base_unique_id}_running"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get the powerwall running state."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return self._coordinator.data[POWERWALL_API_SITEMASTER].running
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device specific state attributes."""
|
|
|
|
return {
|
2020-04-13 19:59:50 +00:00
|
|
|
ATTR_REGION: self._site_info.region,
|
|
|
|
ATTR_GRID_CODE: self._site_info.grid_code,
|
|
|
|
ATTR_NOMINAL_SYSTEM_POWER: self._site_info.nominal_system_power_kW,
|
2020-03-19 15:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PowerWallConnectedSensor(PowerWallEntity, BinarySensorDevice):
|
|
|
|
"""Representation of an Powerwall connected sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
|
|
|
return "Powerwall Connected to Tesla"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_CONNECTIVITY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
|
|
|
return f"{self.base_unique_id}_connected_to_tesla"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get the powerwall connected to tesla state."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return self._coordinator.data[POWERWALL_API_SITEMASTER].connected_to_tesla
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice):
|
|
|
|
"""Representation of an Powerwall grid status sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
|
|
|
return "Grid Status"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_POWER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
|
|
|
return f"{self.base_unique_id}_grid_status"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2020-04-10 16:33:58 +00:00
|
|
|
"""Grid is online."""
|
2020-04-13 19:59:50 +00:00
|
|
|
return self._coordinator.data[POWERWALL_API_GRID_STATUS] == GridStatus.CONNECTED
|
2020-04-20 01:50:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorDevice):
|
|
|
|
"""Representation of an Powerwall grid status sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Device Name."""
|
|
|
|
return "Powerwall Charging"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Device Class."""
|
|
|
|
return DEVICE_CLASS_BATTERY_CHARGING
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Device Uniqueid."""
|
|
|
|
return f"{self.base_unique_id}_powerwall_charging"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Grid is online."""
|
|
|
|
return (
|
|
|
|
self._coordinator.data[POWERWALL_API_METERS][
|
|
|
|
POWERWALL_BATTERY_METER
|
|
|
|
].instant_power
|
|
|
|
< CHARGING_MARGIN_OF_ERROR
|
|
|
|
)
|