Files
homeassistant-core/homeassistant/components/powerwall/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
4.2 KiB
Python
Raw Normal View History

"""Support for powerwall binary sensors."""
2022-02-23 01:15:31 -10:00
from typing import TYPE_CHECKING
2021-10-05 19:40:37 +02:00
from tesla_powerwall import GridStatus, MeterType
2020-03-19 10:50:17 -05:00
from homeassistant.components.binary_sensor import (
2021-12-16 12:38:56 -05:00
BinarySensorDeviceClass,
BinarySensorEntity,
2020-03-19 10:50:17 -05:00
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2020-03-19 10:50:17 -05:00
2022-02-23 01:15:31 -10:00
from .const import DOMAIN
2020-03-19 10:50:17 -05:00
from .entity import PowerWallEntity
2022-02-23 01:15:31 -10:00
from .models import PowerwallRuntimeData
2020-03-19 10:50:17 -05:00
2023-01-24 12:03:15 +11:00
CONNECTED_GRID_STATUSES = {
GridStatus.TRANSITION_TO_GRID,
GridStatus.CONNECTED,
}
2020-03-19 10:50:17 -05:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
2022-02-23 01:15:31 -10:00
"""Set up the powerwall sensors."""
powerwall_data: PowerwallRuntimeData = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
[
sensor_class(powerwall_data)
for sensor_class in (
PowerWallRunningSensor,
PowerWallGridServicesActiveSensor,
PowerWallGridStatusSensor,
PowerWallConnectedSensor,
PowerWallChargingStatusSensor,
)
2022-02-23 01:15:31 -10:00
]
)
2020-03-19 10:50:17 -05:00
class PowerWallRunningSensor(PowerWallEntity, BinarySensorEntity):
2020-03-19 10:50:17 -05:00
"""Representation of an Powerwall running sensor."""
_attr_translation_key = "status"
2022-02-23 01:15:31 -10:00
_attr_device_class = BinarySensorDeviceClass.POWER
2020-03-19 10:50:17 -05:00
@property
2022-02-23 01:15:31 -10:00
def unique_id(self) -> str:
2020-03-19 10:50:17 -05:00
"""Device Uniqueid."""
return f"{self.base_unique_id}_running"
@property
2022-02-23 01:15:31 -10:00
def is_on(self) -> bool:
2020-03-19 10:50:17 -05:00
"""Get the powerwall running state."""
2022-02-23 01:15:31 -10:00
return self.data.site_master.is_running
2020-03-19 10:50:17 -05:00
class PowerWallConnectedSensor(PowerWallEntity, BinarySensorEntity):
2020-03-19 10:50:17 -05:00
"""Representation of an Powerwall connected sensor."""
_attr_translation_key = "connected_to_tesla"
2022-02-23 01:15:31 -10:00
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
2020-03-19 10:50:17 -05:00
@property
2022-02-23 01:15:31 -10:00
def unique_id(self) -> str:
2020-03-19 10:50:17 -05:00
"""Device Uniqueid."""
return f"{self.base_unique_id}_connected_to_tesla"
@property
2022-02-23 01:15:31 -10:00
def is_on(self) -> bool:
2020-03-19 10:50:17 -05:00
"""Get the powerwall connected to tesla state."""
2022-02-23 01:15:31 -10:00
return self.data.site_master.is_connected_to_tesla
2020-03-19 10:50:17 -05:00
class PowerWallGridServicesActiveSensor(PowerWallEntity, BinarySensorEntity):
"""Representation of a Powerwall grid services active sensor."""
_attr_translation_key = "grid_services_active"
2022-02-23 01:15:31 -10:00
_attr_device_class = BinarySensorDeviceClass.POWER
@property
2022-02-23 01:15:31 -10:00
def unique_id(self) -> str:
"""Device Uniqueid."""
return f"{self.base_unique_id}_grid_services_active"
@property
2022-02-23 01:15:31 -10:00
def is_on(self) -> bool:
"""Grid services is active."""
2022-02-23 01:15:31 -10:00
return self.data.grid_services_active
class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorEntity):
2020-03-19 10:50:17 -05:00
"""Representation of an Powerwall grid status sensor."""
_attr_translation_key = "grid_status"
2022-02-23 01:15:31 -10:00
_attr_device_class = BinarySensorDeviceClass.POWER
2020-03-19 10:50:17 -05:00
@property
2022-02-23 01:15:31 -10:00
def unique_id(self) -> str:
2020-03-19 10:50:17 -05:00
"""Device Uniqueid."""
return f"{self.base_unique_id}_grid_status"
@property
2022-02-23 01:15:31 -10:00
def is_on(self) -> bool:
2020-04-10 11:33:58 -05:00
"""Grid is online."""
2023-01-24 12:03:15 +11:00
return self.data.grid_status in CONNECTED_GRID_STATUSES
class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorEntity):
"""Representation of an Powerwall charging status sensor."""
2022-02-23 01:15:31 -10:00
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
@property
def available(self) -> bool:
"""Powerwall is available."""
# Return False if no battery is installed
return (
super().available
and self.data.meters.get_meter(MeterType.BATTERY) is not None
)
@property
2022-02-23 01:15:31 -10:00
def unique_id(self) -> str:
"""Device Uniqueid."""
return f"{self.base_unique_id}_powerwall_charging"
@property
2022-02-23 01:15:31 -10:00
def is_on(self) -> bool:
"""Powerwall is charging."""
meter = self.data.meters.get_meter(MeterType.BATTERY)
# Meter cannot be None because of the available property
if TYPE_CHECKING:
assert meter is not None
# is_sending_to returns true for values greater than 100 watts
return meter.is_sending_to()