Change attributes in new Mercedes Me component (#12147)
* Fix wrong component doc URL * Change attributes to lowercase snakecase * pylint fix * Remove test comments
This commit is contained in:
parent
c72460ccf0
commit
323fe87b57
4 changed files with 56 additions and 62 deletions
|
@ -2,7 +2,7 @@
|
|||
Support for Mercedes cars with Mercedes ME.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/mercedesme/
|
||||
https://home-assistant.io/components/binary_sensor.mercedesme/
|
||||
"""
|
||||
import logging
|
||||
import datetime
|
||||
|
@ -21,14 +21,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
data = hass.data[DATA_MME].data
|
||||
|
||||
if not data.cars:
|
||||
_LOGGER.error("setup_platform data.cars is none")
|
||||
_LOGGER.error("No cars found. Check component log.")
|
||||
return
|
||||
|
||||
devices = []
|
||||
for car in data.cars:
|
||||
for dev in BINARY_SENSORS:
|
||||
for key, value in sorted(BINARY_SENSORS.items()):
|
||||
devices.append(MercedesMEBinarySensor(
|
||||
data, dev, dev, car["vin"], None))
|
||||
data, key, value[0], car["vin"], None))
|
||||
|
||||
add_devices(devices, True)
|
||||
|
||||
|
@ -39,62 +39,56 @@ class MercedesMEBinarySensor(MercedesMeEntity, BinarySensorDevice):
|
|||
@property
|
||||
def is_on(self):
|
||||
"""Return the state of the binary sensor."""
|
||||
return self._state == "On"
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
if self._name == "windowsClosed":
|
||||
if self._internal_name == "windowsClosed":
|
||||
return {
|
||||
"windowStatusFrontLeft": self._car["windowStatusFrontLeft"],
|
||||
"windowStatusFrontRight": self._car["windowStatusFrontRight"],
|
||||
"windowStatusRearLeft": self._car["windowStatusRearLeft"],
|
||||
"windowStatusRearRight": self._car["windowStatusRearRight"],
|
||||
"originalValue": self._car[self._name],
|
||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
||||
"window_front_left": self._car["windowStatusFrontLeft"],
|
||||
"window_front_right": self._car["windowStatusFrontRight"],
|
||||
"window_rear_left": self._car["windowStatusRearLeft"],
|
||||
"window_rear_right": self._car["windowStatusRearRight"],
|
||||
"original_value": self._car[self._internal_name],
|
||||
"last_update": datetime.datetime.fromtimestamp(
|
||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"car": self._car["license"]
|
||||
}
|
||||
elif self._name == "tireWarningLight":
|
||||
elif self._internal_name == "tireWarningLight":
|
||||
return {
|
||||
"frontRightTirePressureKpa":
|
||||
"front_right_tire_pressure_kpa":
|
||||
self._car["frontRightTirePressureKpa"],
|
||||
"frontLeftTirePressureKpa":
|
||||
"front_left_tire_pressure_kpa":
|
||||
self._car["frontLeftTirePressureKpa"],
|
||||
"rearRightTirePressureKpa":
|
||||
"rear_right_tire_pressure_kpa":
|
||||
self._car["rearRightTirePressureKpa"],
|
||||
"rearLeftTirePressureKpa":
|
||||
"rear_left_tire_pressure_kpa":
|
||||
self._car["rearLeftTirePressureKpa"],
|
||||
"originalValue": self._car[self._name],
|
||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
||||
"original_value": self._car[self._internal_name],
|
||||
"last_update": datetime.datetime.fromtimestamp(
|
||||
self._car["lastUpdate"]
|
||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"car": self._car["license"],
|
||||
}
|
||||
return {
|
||||
"originalValue": self._car[self._name],
|
||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
||||
"original_value": self._car[self._internal_name],
|
||||
"last_update": datetime.datetime.fromtimestamp(
|
||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"car": self._car["license"]
|
||||
}
|
||||
|
||||
def update(self):
|
||||
"""Fetch new state data for the sensor."""
|
||||
_LOGGER.debug("Updating %s", self._name)
|
||||
|
||||
self._car = next(
|
||||
car for car in self._data.cars if car["vin"] == self._vin)
|
||||
|
||||
result = False
|
||||
|
||||
if self._name == "windowsClosed":
|
||||
result = bool(self._car[self._name] == "CLOSED")
|
||||
elif self._name == "tireWarningLight":
|
||||
result = bool(self._car[self._name] != "INACTIVE")
|
||||
if self._internal_name == "windowsClosed":
|
||||
self._state = bool(self._car[self._internal_name] == "CLOSED")
|
||||
elif self._internal_name == "tireWarningLight":
|
||||
self._state = bool(self._car[self._internal_name] != "INACTIVE")
|
||||
else:
|
||||
result = self._car[self._name] is True
|
||||
|
||||
self._state = "On" if result else "Off"
|
||||
self._state = self._car[self._internal_name] is True
|
||||
|
||||
_LOGGER.debug("Updated %s Value: %s IsOn: %s",
|
||||
self._name, self._state, self.is_on)
|
||||
self._internal_name, self._state, self.is_on)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
Support for Mercedes cars with Mercedes ME.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/mercedesme/
|
||||
https://home-assistant.io/components/device_tracker.mercedesme/
|
||||
"""
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
@ -38,13 +38,12 @@ class MercedesMEDeviceTracker(object):
|
|||
|
||||
def __init__(self, hass, config, see, data):
|
||||
"""Initialize the Mercedes ME device tracker."""
|
||||
self.hass = hass
|
||||
self.see = see
|
||||
self.data = data
|
||||
self.update_info()
|
||||
|
||||
track_time_interval(
|
||||
self.hass, self.update_info, MIN_TIME_BETWEEN_SCANS)
|
||||
hass, self.update_info, MIN_TIME_BETWEEN_SCANS)
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||
def update_info(self, now=None):
|
||||
|
|
|
@ -12,7 +12,7 @@ import voluptuous as vol
|
|||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_USERNAME, CONF_PASSWORD, CONF_SCAN_INTERVAL)
|
||||
CONF_USERNAME, CONF_PASSWORD, CONF_SCAN_INTERVAL, LENGTH_KILOMETERS)
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect, dispatcher_send)
|
||||
|
@ -23,12 +23,20 @@ REQUIREMENTS = ['mercedesmejsonpy==0.1.2']
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
BINARY_SENSORS = [
|
||||
'doorsClosed',
|
||||
'windowsClosed',
|
||||
'locked',
|
||||
'tireWarningLight'
|
||||
]
|
||||
BINARY_SENSORS = {
|
||||
'doorsClosed': ['Doors closed'],
|
||||
'windowsClosed': ['Windows closed'],
|
||||
'locked': ['Doors locked'],
|
||||
'tireWarningLight': ['Tire Warning']
|
||||
}
|
||||
|
||||
SENSORS = {
|
||||
'fuelLevelPercent': ['Fuel Level', '%'],
|
||||
'fuelRangeKm': ['Fuel Range', LENGTH_KILOMETERS],
|
||||
'latestTrip': ['Latest Trip', None],
|
||||
'odometerKm': ['Odometer', LENGTH_KILOMETERS],
|
||||
'serviceIntervalDays': ['Next Service', 'days']
|
||||
}
|
||||
|
||||
DATA_MME = 'mercedesme'
|
||||
DOMAIN = 'mercedesme'
|
||||
|
@ -136,6 +144,8 @@ class MercedesMeEntity(Entity):
|
|||
|
||||
def _update_callback(self):
|
||||
"""Callback update method."""
|
||||
# If the method is made a callback this should be changed
|
||||
# to the async version. Check core.callback
|
||||
self.schedule_update_ha_state(True)
|
||||
|
||||
@property
|
||||
|
|
|
@ -2,28 +2,19 @@
|
|||
Support for Mercedes cars with Mercedes ME.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/mercedesme/
|
||||
https://home-assistant.io/components/sensor.mercedesme/
|
||||
"""
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
from homeassistant.const import LENGTH_KILOMETERS
|
||||
from homeassistant.components.mercedesme import DATA_MME, MercedesMeEntity
|
||||
from homeassistant.components.mercedesme import (
|
||||
DATA_MME, MercedesMeEntity, SENSORS)
|
||||
|
||||
|
||||
DEPENDENCIES = ['mercedesme']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SENSOR_TYPES = {
|
||||
'fuelLevelPercent': ['Fuel Level', '%'],
|
||||
'fuelRangeKm': ['Fuel Range', LENGTH_KILOMETERS],
|
||||
'latestTrip': ['Latest Trip', None],
|
||||
'odometerKm': ['Odometer', LENGTH_KILOMETERS],
|
||||
'serviceIntervalDays': ['Next Service', 'days'],
|
||||
'doorsClosed': ['doorsClosed', None],
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the sensor platform."""
|
||||
|
@ -37,7 +28,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
|
||||
devices = []
|
||||
for car in data.cars:
|
||||
for key, value in sorted(SENSOR_TYPES.items()):
|
||||
for key, value in sorted(SENSORS.items()):
|
||||
devices.append(
|
||||
MercedesMESensor(data, key, value[0], car["vin"], value[1]))
|
||||
|
||||
|
@ -69,24 +60,24 @@ class MercedesMESensor(MercedesMeEntity):
|
|||
"""Return the state attributes."""
|
||||
if self._internal_name == "latestTrip":
|
||||
return {
|
||||
"durationSeconds":
|
||||
"duration_seconds":
|
||||
self._car["latestTrip"]["durationSeconds"],
|
||||
"distanceTraveledKm":
|
||||
"distance_traveled_km":
|
||||
self._car["latestTrip"]["distanceTraveledKm"],
|
||||
"startedAt": datetime.datetime.fromtimestamp(
|
||||
"started_at": datetime.datetime.fromtimestamp(
|
||||
self._car["latestTrip"]["startedAt"]
|
||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"averageSpeedKmPerHr":
|
||||
"average_speed_km_per_hr":
|
||||
self._car["latestTrip"]["averageSpeedKmPerHr"],
|
||||
"finished": self._car["latestTrip"]["finished"],
|
||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
||||
"last_update": datetime.datetime.fromtimestamp(
|
||||
self._car["lastUpdate"]
|
||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"car": self._car["license"]
|
||||
}
|
||||
|
||||
return {
|
||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
||||
"last_update": datetime.datetime.fromtimestamp(
|
||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||
"car": self._car["license"]
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue