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.
|
Support for Mercedes cars with Mercedes ME.
|
||||||
|
|
||||||
For more details about this component, please refer to the documentation at
|
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 logging
|
||||||
import datetime
|
import datetime
|
||||||
|
@ -21,14 +21,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
data = hass.data[DATA_MME].data
|
data = hass.data[DATA_MME].data
|
||||||
|
|
||||||
if not data.cars:
|
if not data.cars:
|
||||||
_LOGGER.error("setup_platform data.cars is none")
|
_LOGGER.error("No cars found. Check component log.")
|
||||||
return
|
return
|
||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
for car in data.cars:
|
for car in data.cars:
|
||||||
for dev in BINARY_SENSORS:
|
for key, value in sorted(BINARY_SENSORS.items()):
|
||||||
devices.append(MercedesMEBinarySensor(
|
devices.append(MercedesMEBinarySensor(
|
||||||
data, dev, dev, car["vin"], None))
|
data, key, value[0], car["vin"], None))
|
||||||
|
|
||||||
add_devices(devices, True)
|
add_devices(devices, True)
|
||||||
|
|
||||||
|
@ -39,62 +39,56 @@ class MercedesMEBinarySensor(MercedesMeEntity, BinarySensorDevice):
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return the state of the binary sensor."""
|
"""Return the state of the binary sensor."""
|
||||||
return self._state == "On"
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self._name == "windowsClosed":
|
if self._internal_name == "windowsClosed":
|
||||||
return {
|
return {
|
||||||
"windowStatusFrontLeft": self._car["windowStatusFrontLeft"],
|
"window_front_left": self._car["windowStatusFrontLeft"],
|
||||||
"windowStatusFrontRight": self._car["windowStatusFrontRight"],
|
"window_front_right": self._car["windowStatusFrontRight"],
|
||||||
"windowStatusRearLeft": self._car["windowStatusRearLeft"],
|
"window_rear_left": self._car["windowStatusRearLeft"],
|
||||||
"windowStatusRearRight": self._car["windowStatusRearRight"],
|
"window_rear_right": self._car["windowStatusRearRight"],
|
||||||
"originalValue": self._car[self._name],
|
"original_value": self._car[self._internal_name],
|
||||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
"last_update": datetime.datetime.fromtimestamp(
|
||||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"car": self._car["license"]
|
"car": self._car["license"]
|
||||||
}
|
}
|
||||||
elif self._name == "tireWarningLight":
|
elif self._internal_name == "tireWarningLight":
|
||||||
return {
|
return {
|
||||||
"frontRightTirePressureKpa":
|
"front_right_tire_pressure_kpa":
|
||||||
self._car["frontRightTirePressureKpa"],
|
self._car["frontRightTirePressureKpa"],
|
||||||
"frontLeftTirePressureKpa":
|
"front_left_tire_pressure_kpa":
|
||||||
self._car["frontLeftTirePressureKpa"],
|
self._car["frontLeftTirePressureKpa"],
|
||||||
"rearRightTirePressureKpa":
|
"rear_right_tire_pressure_kpa":
|
||||||
self._car["rearRightTirePressureKpa"],
|
self._car["rearRightTirePressureKpa"],
|
||||||
"rearLeftTirePressureKpa":
|
"rear_left_tire_pressure_kpa":
|
||||||
self._car["rearLeftTirePressureKpa"],
|
self._car["rearLeftTirePressureKpa"],
|
||||||
"originalValue": self._car[self._name],
|
"original_value": self._car[self._internal_name],
|
||||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
"last_update": datetime.datetime.fromtimestamp(
|
||||||
self._car["lastUpdate"]
|
self._car["lastUpdate"]
|
||||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"car": self._car["license"],
|
"car": self._car["license"],
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
"originalValue": self._car[self._name],
|
"original_value": self._car[self._internal_name],
|
||||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
"last_update": datetime.datetime.fromtimestamp(
|
||||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"car": self._car["license"]
|
"car": self._car["license"]
|
||||||
}
|
}
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
_LOGGER.debug("Updating %s", self._name)
|
|
||||||
|
|
||||||
self._car = next(
|
self._car = next(
|
||||||
car for car in self._data.cars if car["vin"] == self._vin)
|
car for car in self._data.cars if car["vin"] == self._vin)
|
||||||
|
|
||||||
result = False
|
if self._internal_name == "windowsClosed":
|
||||||
|
self._state = bool(self._car[self._internal_name] == "CLOSED")
|
||||||
if self._name == "windowsClosed":
|
elif self._internal_name == "tireWarningLight":
|
||||||
result = bool(self._car[self._name] == "CLOSED")
|
self._state = bool(self._car[self._internal_name] != "INACTIVE")
|
||||||
elif self._name == "tireWarningLight":
|
|
||||||
result = bool(self._car[self._name] != "INACTIVE")
|
|
||||||
else:
|
else:
|
||||||
result = self._car[self._name] is True
|
self._state = self._car[self._internal_name] is True
|
||||||
|
|
||||||
self._state = "On" if result else "Off"
|
|
||||||
|
|
||||||
_LOGGER.debug("Updated %s Value: %s IsOn: %s",
|
_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.
|
Support for Mercedes cars with Mercedes ME.
|
||||||
|
|
||||||
For more details about this component, please refer to the documentation at
|
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
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
@ -38,13 +38,12 @@ class MercedesMEDeviceTracker(object):
|
||||||
|
|
||||||
def __init__(self, hass, config, see, data):
|
def __init__(self, hass, config, see, data):
|
||||||
"""Initialize the Mercedes ME device tracker."""
|
"""Initialize the Mercedes ME device tracker."""
|
||||||
self.hass = hass
|
|
||||||
self.see = see
|
self.see = see
|
||||||
self.data = data
|
self.data = data
|
||||||
self.update_info()
|
self.update_info()
|
||||||
|
|
||||||
track_time_interval(
|
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)
|
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||||
def update_info(self, now=None):
|
def update_info(self, now=None):
|
||||||
|
|
|
@ -12,7 +12,7 @@ import voluptuous as vol
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from homeassistant.const import (
|
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 import discovery
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect, dispatcher_send)
|
async_dispatcher_connect, dispatcher_send)
|
||||||
|
@ -23,12 +23,20 @@ REQUIREMENTS = ['mercedesmejsonpy==0.1.2']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
BINARY_SENSORS = [
|
BINARY_SENSORS = {
|
||||||
'doorsClosed',
|
'doorsClosed': ['Doors closed'],
|
||||||
'windowsClosed',
|
'windowsClosed': ['Windows closed'],
|
||||||
'locked',
|
'locked': ['Doors locked'],
|
||||||
'tireWarningLight'
|
'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'
|
DATA_MME = 'mercedesme'
|
||||||
DOMAIN = 'mercedesme'
|
DOMAIN = 'mercedesme'
|
||||||
|
@ -136,6 +144,8 @@ class MercedesMeEntity(Entity):
|
||||||
|
|
||||||
def _update_callback(self):
|
def _update_callback(self):
|
||||||
"""Callback update method."""
|
"""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)
|
self.schedule_update_ha_state(True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -2,28 +2,19 @@
|
||||||
Support for Mercedes cars with Mercedes ME.
|
Support for Mercedes cars with Mercedes ME.
|
||||||
|
|
||||||
For more details about this component, please refer to the documentation at
|
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 logging
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from homeassistant.const import LENGTH_KILOMETERS
|
from homeassistant.components.mercedesme import (
|
||||||
from homeassistant.components.mercedesme import DATA_MME, MercedesMeEntity
|
DATA_MME, MercedesMeEntity, SENSORS)
|
||||||
|
|
||||||
|
|
||||||
DEPENDENCIES = ['mercedesme']
|
DEPENDENCIES = ['mercedesme']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the sensor platform."""
|
"""Setup the sensor platform."""
|
||||||
|
@ -37,7 +28,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
for car in data.cars:
|
for car in data.cars:
|
||||||
for key, value in sorted(SENSOR_TYPES.items()):
|
for key, value in sorted(SENSORS.items()):
|
||||||
devices.append(
|
devices.append(
|
||||||
MercedesMESensor(data, key, value[0], car["vin"], value[1]))
|
MercedesMESensor(data, key, value[0], car["vin"], value[1]))
|
||||||
|
|
||||||
|
@ -69,24 +60,24 @@ class MercedesMESensor(MercedesMeEntity):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self._internal_name == "latestTrip":
|
if self._internal_name == "latestTrip":
|
||||||
return {
|
return {
|
||||||
"durationSeconds":
|
"duration_seconds":
|
||||||
self._car["latestTrip"]["durationSeconds"],
|
self._car["latestTrip"]["durationSeconds"],
|
||||||
"distanceTraveledKm":
|
"distance_traveled_km":
|
||||||
self._car["latestTrip"]["distanceTraveledKm"],
|
self._car["latestTrip"]["distanceTraveledKm"],
|
||||||
"startedAt": datetime.datetime.fromtimestamp(
|
"started_at": datetime.datetime.fromtimestamp(
|
||||||
self._car["latestTrip"]["startedAt"]
|
self._car["latestTrip"]["startedAt"]
|
||||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"averageSpeedKmPerHr":
|
"average_speed_km_per_hr":
|
||||||
self._car["latestTrip"]["averageSpeedKmPerHr"],
|
self._car["latestTrip"]["averageSpeedKmPerHr"],
|
||||||
"finished": self._car["latestTrip"]["finished"],
|
"finished": self._car["latestTrip"]["finished"],
|
||||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
"last_update": datetime.datetime.fromtimestamp(
|
||||||
self._car["lastUpdate"]
|
self._car["lastUpdate"]
|
||||||
).strftime('%Y-%m-%d %H:%M:%S'),
|
).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"car": self._car["license"]
|
"car": self._car["license"]
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"lastUpdate": datetime.datetime.fromtimestamp(
|
"last_update": datetime.datetime.fromtimestamp(
|
||||||
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
self._car["lastUpdate"]).strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"car": self._car["license"]
|
"car": self._car["license"]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue