core/homeassistant/components/wallbox/sensor.py

211 lines
7.2 KiB
Python
Raw Normal View History

Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
"""Home Assistant component for accessing the Wallbox Portal API. The sensor component creates multiple sensors regarding wallbox performance."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import cast
from homeassistant.components.sensor import (
2021-12-17 11:46:21 +00:00
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
2021-12-17 11:46:21 +00:00
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE,
ENERGY_KILO_WATT_HOUR,
LENGTH_KILOMETERS,
PERCENTAGE,
POWER_KILO_WATT,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
from . import WallboxCoordinator, WallboxEntity
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
from .const import (
CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
CHARGER_ADDED_ENERGY_KEY,
CHARGER_ADDED_RANGE_KEY,
CHARGER_CHARGING_POWER_KEY,
CHARGER_CHARGING_SPEED_KEY,
CHARGER_COST_KEY,
CHARGER_CURRENCY_KEY,
CHARGER_CURRENT_MODE_KEY,
CHARGER_DATA_KEY,
CHARGER_DEPOT_PRICE_KEY,
CHARGER_ENERGY_PRICE_KEY,
CHARGER_MAX_AVAILABLE_POWER_KEY,
CHARGER_MAX_CHARGING_CURRENT_KEY,
CHARGER_SERIAL_NUMBER_KEY,
CHARGER_STATE_OF_CHARGE_KEY,
CHARGER_STATUS_DESCRIPTION_KEY,
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
DOMAIN,
)
CHARGER_STATION = "station"
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
UPDATE_INTERVAL = 30
_LOGGER = logging.getLogger(__name__)
@dataclass
class WallboxSensorEntityDescription(SensorEntityDescription):
"""Describes Wallbox sensor entity."""
precision: int | None = None
SENSOR_TYPES: dict[str, WallboxSensorEntityDescription] = {
CHARGER_CHARGING_POWER_KEY: WallboxSensorEntityDescription(
key=CHARGER_CHARGING_POWER_KEY,
name="Charging Power",
precision=2,
native_unit_of_measurement=POWER_KILO_WATT,
2021-12-17 11:46:21 +00:00
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_MAX_AVAILABLE_POWER_KEY: WallboxSensorEntityDescription(
key=CHARGER_MAX_AVAILABLE_POWER_KEY,
name="Max Available Power",
precision=0,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
2021-12-17 11:46:21 +00:00
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_CHARGING_SPEED_KEY: WallboxSensorEntityDescription(
key=CHARGER_CHARGING_SPEED_KEY,
icon="mdi:speedometer",
name="Charging Speed",
precision=0,
2021-12-17 11:46:21 +00:00
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_ADDED_RANGE_KEY: WallboxSensorEntityDescription(
key=CHARGER_ADDED_RANGE_KEY,
icon="mdi:map-marker-distance",
name="Added Range",
precision=0,
native_unit_of_measurement=LENGTH_KILOMETERS,
device_class=SensorDeviceClass.DISTANCE,
2021-12-17 11:46:21 +00:00
state_class=SensorStateClass.TOTAL_INCREASING,
),
CHARGER_ADDED_ENERGY_KEY: WallboxSensorEntityDescription(
key=CHARGER_ADDED_ENERGY_KEY,
name="Added Energy",
precision=2,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
2021-12-17 11:46:21 +00:00
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
CHARGER_ADDED_DISCHARGED_ENERGY_KEY: WallboxSensorEntityDescription(
key=CHARGER_ADDED_DISCHARGED_ENERGY_KEY,
name="Discharged Energy",
precision=2,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
CHARGER_COST_KEY: WallboxSensorEntityDescription(
key=CHARGER_COST_KEY,
icon="mdi:ev-station",
name="Cost",
2021-12-17 11:46:21 +00:00
state_class=SensorStateClass.TOTAL_INCREASING,
),
CHARGER_STATE_OF_CHARGE_KEY: WallboxSensorEntityDescription(
key=CHARGER_STATE_OF_CHARGE_KEY,
name="State of Charge",
native_unit_of_measurement=PERCENTAGE,
2021-12-17 11:46:21 +00:00
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_CURRENT_MODE_KEY: WallboxSensorEntityDescription(
key=CHARGER_CURRENT_MODE_KEY,
icon="mdi:ev-station",
name="Current Mode",
),
CHARGER_DEPOT_PRICE_KEY: WallboxSensorEntityDescription(
key=CHARGER_DEPOT_PRICE_KEY,
icon="mdi:ev-station",
name="Depot Price",
precision=2,
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_ENERGY_PRICE_KEY: WallboxSensorEntityDescription(
key=CHARGER_ENERGY_PRICE_KEY,
icon="mdi:ev-station",
name="Energy Price",
precision=2,
state_class=SensorStateClass.MEASUREMENT,
),
CHARGER_STATUS_DESCRIPTION_KEY: WallboxSensorEntityDescription(
key=CHARGER_STATUS_DESCRIPTION_KEY,
icon="mdi:ev-station",
name="Status Description",
),
CHARGER_MAX_CHARGING_CURRENT_KEY: WallboxSensorEntityDescription(
key=CHARGER_MAX_CHARGING_CURRENT_KEY,
name="Max. Charging Current",
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
2021-12-17 11:46:21 +00:00
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
),
}
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
"""Create wallbox sensor entities in HASS."""
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
async_add_entities(
[
WallboxSensor(coordinator, entry, description)
for ent in coordinator.data
if (description := SENSOR_TYPES.get(ent))
]
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
)
class WallboxSensor(WallboxEntity, SensorEntity):
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
"""Representation of the Wallbox portal."""
entity_description: WallboxSensorEntityDescription
def __init__(
self,
coordinator: WallboxCoordinator,
entry: ConfigEntry,
description: WallboxSensorEntityDescription,
) -> None:
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
"""Initialize a Wallbox sensor."""
super().__init__(coordinator)
self.entity_description = description
self._attr_name = f"{entry.title} {description.name}"
self._attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
Add wallbox integration (#48082) * Wallbox component added * resolved mergeconflicts from upstream * fixed an incorrect removal in CODEOWNERS file * fixes for pullrequest automatic test * clean up code after PR tests * fixed strings.json * fix config_flow error > wallbox * fixed some formatting issues * fix pylint warnings * fixed error in number.py > set value * pylint warnings fixed * some more pylint fixes * isort fixes * fix unused_import pylint * remove tests * remove test requirements * config flow test * test errors resolved * test file formatting * isort on test file * sensor test * isort on test * isort test const * remove not working sensor test * remove test const * add switch, number and lock test * docstrings for test classes * sort test_number, create test_sensor * additional tests * fix test error * reduced PR to 1 component * newline in const * ignore test coverage -> dependency on external device (wallbox) * do not ignore config_flow * add test for validate_input * remove obsolete import * additional test config flow * change test sensor * docstring * add additional test for exceptions * fix test_config * more tests * fix test_config_flow * fixed http error test * catch connectionerror and introduce testing for this error * remove .coveragefile * change comment * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen Co-authored-by: jan iversen <jancasacondor@gmail.com> * Update homeassistant/components/wallbox/__init__.py review suggestion by janiversen (format only) Co-authored-by: jan iversen <jancasacondor@gmail.com> * Processed review comments, include more testing for sensor component * Isolated the async_add_executor_job to make the solution more async * add a config flow test * Revert "add a config flow test" This reverts commit 9c1af82fffeb0b46f11ada1000e19b66fd5fd0f1. * Revert "Isolated the async_add_executor_job to make the solution more async" This reverts commit 0bf034c3318f27e649389830d4ad7a7e10eb2d6f. * Make component more async and add config flow tests * Changes based on review comments * made _ methods in WallboxHub for the 'non-async' call to the API and try-catch. Stored the wallbox in the class. * moved the coordinator to __init__ and pass it as part of the WallboxHub class * removed obsolete function in __init__ * removed CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL * fixed spelling and imports on test files * did isort on component files Co-authored-by: jan iversen <jancasacondor@gmail.com>
2021-05-24 11:08:24 +00:00
@property
def native_value(self) -> StateType:
2022-06-10 18:55:55 +00:00
"""Return the state of the sensor. Round the value when it, and the precision property are not None."""
if (
sensor_round := self.entity_description.precision
) is not None and self.coordinator.data[
self.entity_description.key
] is not None:
return cast(
StateType,
round(self.coordinator.data[self.entity_description.key], sensor_round),
)
return cast(StateType, self.coordinator.data[self.entity_description.key])
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of the sensor. When monetary, get the value from the api."""
if self.entity_description.key in (
CHARGER_ENERGY_PRICE_KEY,
CHARGER_DEPOT_PRICE_KEY,
):
return cast(str, self.coordinator.data[CHARGER_CURRENCY_KEY])
return cast(str, self.entity_description.native_unit_of_measurement)