2019-02-14 04:35:12 +00:00
|
|
|
"""Support for monitoring juicenet/juicepoint/juicebox based EVSE sensors."""
|
2021-08-10 02:25:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2020-04-11 00:04:58 +00:00
|
|
|
from homeassistant.const import (
|
2021-07-12 18:32:55 +00:00
|
|
|
DEVICE_CLASS_CURRENT,
|
|
|
|
DEVICE_CLASS_ENERGY,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
DEVICE_CLASS_VOLTAGE,
|
2021-07-20 18:06:23 +00:00
|
|
|
ELECTRIC_CURRENT_AMPERE,
|
|
|
|
ELECTRIC_POTENTIAL_VOLT,
|
2020-04-11 00:04:58 +00:00
|
|
|
ENERGY_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TIME_SECONDS,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-05-08 05:52:20 +00:00
|
|
|
from .const import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
|
|
|
|
from .entity import JuiceNetDevice
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2021-08-10 02:25:22 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="status",
|
|
|
|
name="Charging Status",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="temperature",
|
|
|
|
name="Temperature",
|
|
|
|
unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
icon="mdi:thermometer",
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="voltage",
|
|
|
|
name="Voltage",
|
|
|
|
unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
|
|
|
|
icon="mdi:flash",
|
|
|
|
device_class=DEVICE_CLASS_VOLTAGE,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="amps",
|
|
|
|
name="Amps",
|
|
|
|
unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
|
|
|
|
icon="mdi:flash",
|
|
|
|
device_class=DEVICE_CLASS_CURRENT,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="watts",
|
|
|
|
name="Watts",
|
|
|
|
unit_of_measurement=POWER_WATT,
|
|
|
|
icon="mdi:flash",
|
|
|
|
device_class=DEVICE_CLASS_POWER,
|
|
|
|
state_class=STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="charge_time",
|
|
|
|
name="Charge time",
|
|
|
|
unit_of_measurement=TIME_SECONDS,
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="energy_added",
|
|
|
|
name="Energy added",
|
|
|
|
unit_of_measurement=ENERGY_WATT_HOUR,
|
|
|
|
icon="mdi:flash",
|
|
|
|
device_class=DEVICE_CLASS_ENERGY,
|
|
|
|
),
|
|
|
|
)
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
|
2020-05-08 05:52:20 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the JuiceNet Sensors."""
|
|
|
|
juicenet_data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
api = juicenet_data[JUICENET_API]
|
|
|
|
coordinator = juicenet_data[JUICENET_COORDINATOR]
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2021-08-10 02:25:22 +00:00
|
|
|
entities = [
|
|
|
|
JuiceNetSensorDevice(device, coordinator, description)
|
|
|
|
for device in api.devices
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
]
|
2020-05-08 05:52:20 +00:00
|
|
|
async_add_entities(entities)
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class JuiceNetSensorDevice(JuiceNetDevice, SensorEntity):
|
2020-05-08 05:52:20 +00:00
|
|
|
"""Implementation of a JuiceNet sensor."""
|
2017-06-05 15:39:31 +00:00
|
|
|
|
2021-08-10 02:25:22 +00:00
|
|
|
def __init__(self, device, coordinator, description: SensorEntityDescription):
|
2017-06-05 15:39:31 +00:00
|
|
|
"""Initialise the sensor."""
|
2021-08-10 02:25:22 +00:00
|
|
|
super().__init__(device, description.key, coordinator)
|
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{self.device.name} {description.name}"
|
2017-06-05 15:39:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon of the sensor."""
|
|
|
|
icon = None
|
2021-08-10 02:25:22 +00:00
|
|
|
if self.entity_description.key == "status":
|
2020-05-08 05:52:20 +00:00
|
|
|
status = self.device.status
|
2019-07-31 19:25:30 +00:00
|
|
|
if status == "standby":
|
|
|
|
icon = "mdi:power-plug-off"
|
|
|
|
elif status == "plugged":
|
|
|
|
icon = "mdi:power-plug"
|
|
|
|
elif status == "charging":
|
|
|
|
icon = "mdi:battery-positive"
|
2021-08-10 02:25:22 +00:00
|
|
|
else:
|
|
|
|
icon = self.entity_description.icon
|
2017-06-05 15:39:31 +00:00
|
|
|
return icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
state = None
|
2021-08-10 02:25:22 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == "status":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.status
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "temperature":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.temperature
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "voltage":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.voltage
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "amps":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.amps
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "watts":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.watts
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "charge_time":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.charge_time
|
2021-08-10 02:25:22 +00:00
|
|
|
elif sensor_type == "energy_added":
|
2020-05-08 05:52:20 +00:00
|
|
|
state = self.device.energy_added
|
2017-06-05 15:39:31 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
state = "Unknown"
|
2017-06-05 15:39:31 +00:00
|
|
|
return state
|