hass-core/homeassistant/components/keba/sensor.py
Philipp Danner 75e18d4282 Add Keba charging station/wallbox as component (#24484)
* Add Keba charging station wallbox component

* Added start/stop commands (ena 0 and ena 1)

* added refresh_interval parameter and fixed authorization

* fixed max line length

* deactivate failsafe mode if not set in configuration

* extracted I/O code to pypi library

* updated services.yaml

* pinned version of requirements

* fixed typos, indent and comments

* simplified sensor generation, fixed unique_id and name of sensors

* cleaned up data extraction

* flake8 fixes

* added fast polling, fixed unique_id, code cleanup

* updated requirements

* fixed pylint

* integrated code styling suggestions

* fixed pylint

* code style changes according to suggestions and pylint fixes

* formatted with black

* clarefied variables

* Update homeassistant/components/keba/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>

* Update homeassistant/components/keba/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>

* Update homeassistant/components/keba/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>

* Update homeassistant/components/keba/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>

* fixed behaviour if no charging station was found

* fix pylint

* Update homeassistant/components/keba/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@kth.se>
2019-08-19 14:29:26 +02:00

109 lines
3.5 KiB
Python

"""Support for KEBA charging station sensors."""
import logging
from homeassistant.const import ENERGY_KILO_WATT_HOUR
from homeassistant.helpers.entity import Entity
from homeassistant.const import DEVICE_CLASS_POWER
from . import DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the KEBA charging station platform."""
if discovery_info is None:
return
keba = hass.data[DOMAIN]
sensors = [
KebaSensor(keba, "Curr user", "Max current", "mdi:flash", "A"),
KebaSensor(
keba, "Setenergy", "Energy target", "mdi:gauge", ENERGY_KILO_WATT_HOUR
),
KebaSensor(keba, "P", "Charging power", "mdi:flash", "kW", DEVICE_CLASS_POWER),
KebaSensor(
keba, "E pres", "Session energy", "mdi:gauge", ENERGY_KILO_WATT_HOUR
),
KebaSensor(keba, "E total", "Total Energy", "mdi:gauge", ENERGY_KILO_WATT_HOUR),
]
async_add_entities(sensors)
class KebaSensor(Entity):
"""The entity class for KEBA charging stations sensors."""
def __init__(self, keba, key, name, icon, unit, device_class=None):
"""Initialize the KEBA Sensor."""
self._key = key
self._keba = keba
self._name = name
self._device_class = device_class
self._icon = icon
self._unit = unit
self._state = None
self._attributes = {}
@property
def should_poll(self):
"""Deactivate polling. Data updated by KebaHandler."""
return False
@property
def unique_id(self):
"""Return the unique ID of the binary sensor."""
return f"{self._keba.device_name}_{self._name}"
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def device_class(self):
"""Return the class of this sensor."""
return self._device_class
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Get the unit of measurement."""
return self._unit
@property
def device_state_attributes(self):
"""Return the state attributes of the binary sensor."""
return self._attributes
async def async_update(self):
"""Get latest cached states from the device."""
self._state = self._keba.get_value(self._key)
if self._key == "P":
self._attributes["power_factor"] = self._keba.get_value("PF")
self._attributes["voltage_u1"] = str(self._keba.get_value("U1"))
self._attributes["voltage_u2"] = str(self._keba.get_value("U2"))
self._attributes["voltage_u3"] = str(self._keba.get_value("U3"))
self._attributes["current_i1"] = str(self._keba.get_value("I1"))
self._attributes["current_i2"] = str(self._keba.get_value("I2"))
self._attributes["current_i3"] = str(self._keba.get_value("I3"))
elif self._key == "Curr user":
self._attributes["max_current_hardware"] = self._keba.get_value("Curr HW")
def update_callback(self):
"""Schedule a state update."""
self.async_schedule_update_ha_state(True)
async def async_added_to_hass(self):
"""Add update callback after being added to hass."""
self._keba.add_update_listener(self.update_callback)