From 5e90a4d000f4db4975b5f5db1de2b838121a5220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 22 Sep 2020 00:03:39 +0300 Subject: [PATCH] Use more state attribute name constants (#40428) --- .../alarm_control_panel/device_action.py | 3 +- .../alarm_control_panel/device_condition.py | 3 +- .../alarm_control_panel/device_trigger.py | 3 +- homeassistant/components/alexa/handlers.py | 3 +- .../components/climate/device_action.py | 3 +- .../components/climate/device_condition.py | 6 +- homeassistant/components/dweet/__init__.py | 3 +- .../homekit/type_security_systems.py | 3 +- .../components/humidifier/device_action.py | 3 +- .../components/humidifier/device_condition.py | 3 +- .../components/mobile_app/webhook.py | 3 +- .../components/owntracks/__init__.py | 13 +++-- .../components/owntracks/messages.py | 7 ++- .../components/prometheus/__init__.py | 6 +- .../components/proximity/__init__.py | 18 +++--- tests/components/august/test_sensor.py | 6 +- .../components/config/test_entity_registry.py | 5 +- tests/components/demo/test_geo_location.py | 18 ++++-- tests/components/demo/test_media_player.py | 3 +- tests/components/dynalite/test_cover.py | 6 +- tests/components/dynalite/test_light.py | 5 +- tests/components/dynalite/test_switch.py | 4 +- tests/components/eafm/test_sensor.py | 11 ++-- tests/components/geofency/test_init.py | 6 +- tests/components/rflink/test_sensor.py | 13 +++-- tests/components/rfxtrx/test_sensor.py | 54 +++++++++--------- tests/components/sma/test_sensor.py | 4 +- tests/components/spaceapi/test_init.py | 8 +-- tests/components/startca/test_sensor.py | 55 ++++++++++--------- tests/components/statistics/test_sensor.py | 2 +- tests/components/teksavvy/test_sensor.py | 47 +++++++++------- tests/components/utility_meter/test_init.py | 11 ++-- tests/components/utility_meter/test_sensor.py | 27 +++++---- tests/components/vera/test_sensor.py | 6 +- tests/components/wunderground/test_sensor.py | 13 +++-- tests/helpers/test_template.py | 3 +- 36 files changed, 231 insertions(+), 156 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/device_action.py b/homeassistant/components/alarm_control_panel/device_action.py index 81e444ae16f..0dc16fdcf42 100644 --- a/homeassistant/components/alarm_control_panel/device_action.py +++ b/homeassistant/components/alarm_control_panel/device_action.py @@ -6,6 +6,7 @@ import voluptuous as vol from homeassistant.const import ( ATTR_CODE, ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_CODE, CONF_DEVICE_ID, CONF_DOMAIN, @@ -56,7 +57,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]: if state is None: continue - supported_features = state.attributes["supported_features"] + supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] # Add actions for each entity that belongs to this integration if supported_features & SUPPORT_ALARM_ARM_AWAY: diff --git a/homeassistant/components/alarm_control_panel/device_condition.py b/homeassistant/components/alarm_control_panel/device_condition.py index c4d43d1b051..e5b3ec6aeee 100644 --- a/homeassistant/components/alarm_control_panel/device_condition.py +++ b/homeassistant/components/alarm_control_panel/device_condition.py @@ -11,6 +11,7 @@ from homeassistant.components.alarm_control_panel.const import ( ) from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_CONDITION, CONF_DEVICE_ID, CONF_DOMAIN, @@ -73,7 +74,7 @@ async def async_get_conditions( if state is None: continue - supported_features = state.attributes["supported_features"] + supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] # Add conditions for each entity that belongs to this integration conditions += [ diff --git a/homeassistant/components/alarm_control_panel/device_trigger.py b/homeassistant/components/alarm_control_panel/device_trigger.py index eeea1dbbf33..cb07ff35e96 100644 --- a/homeassistant/components/alarm_control_panel/device_trigger.py +++ b/homeassistant/components/alarm_control_panel/device_trigger.py @@ -12,6 +12,7 @@ from homeassistant.components.automation import AutomationActionType from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.const import ( + ATTR_SUPPORTED_FEATURES, CONF_DEVICE_ID, CONF_DOMAIN, CONF_ENTITY_ID, @@ -64,7 +65,7 @@ async def async_get_triggers(hass: HomeAssistant, device_id: str) -> List[dict]: if entity_state is None: continue - supported_features = entity_state.attributes["supported_features"] + supported_features = entity_state.attributes[ATTR_SUPPORTED_FEATURES] # Add triggers for each entity that belongs to this integration triggers += [ diff --git a/homeassistant/components/alexa/handlers.py b/homeassistant/components/alexa/handlers.py index 6eeb3235a64..783c7a36949 100644 --- a/homeassistant/components/alexa/handlers.py +++ b/homeassistant/components/alexa/handlers.py @@ -17,6 +17,7 @@ from homeassistant.components import ( from homeassistant.components.climate import const as climate from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_ENTITY_PICTURE, ATTR_SUPPORTED_FEATURES, ATTR_TEMPERATURE, SERVICE_ALARM_ARM_AWAY, @@ -1532,7 +1533,7 @@ async def async_api_initialize_camera_stream(hass, config, directive, context): """Process a InitializeCameraStreams request.""" entity = directive.entity stream_source = await camera.async_request_stream(hass, entity.entity_id, fmt="hls") - camera_image = hass.states.get(entity.entity_id).attributes["entity_picture"] + camera_image = hass.states.get(entity.entity_id).attributes[ATTR_ENTITY_PICTURE] try: external_url = network.get_url( diff --git a/homeassistant/components/climate/device_action.py b/homeassistant/components/climate/device_action.py index 6f7725ac835..3f2b8dc23f2 100644 --- a/homeassistant/components/climate/device_action.py +++ b/homeassistant/components/climate/device_action.py @@ -5,6 +5,7 @@ import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_DEVICE_ID, CONF_DOMAIN, CONF_ENTITY_ID, @@ -61,7 +62,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]: CONF_TYPE: "set_hvac_mode", } ) - if state.attributes["supported_features"] & const.SUPPORT_PRESET_MODE: + if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE: actions.append( { CONF_DEVICE_ID: device_id, diff --git a/homeassistant/components/climate/device_condition.py b/homeassistant/components/climate/device_condition.py index 8a5b9ceede8..423efdf8196 100644 --- a/homeassistant/components/climate/device_condition.py +++ b/homeassistant/components/climate/device_condition.py @@ -5,6 +5,7 @@ import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_CONDITION, CONF_DEVICE_ID, CONF_DOMAIN, @@ -63,7 +64,10 @@ async def async_get_conditions( } ) - if state and state.attributes["supported_features"] & const.SUPPORT_PRESET_MODE: + if ( + state + and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE + ): conditions.append( { CONF_CONDITION: "device", diff --git a/homeassistant/components/dweet/__init__.py b/homeassistant/components/dweet/__init__.py index db985e57a41..c076fc81628 100644 --- a/homeassistant/components/dweet/__init__.py +++ b/homeassistant/components/dweet/__init__.py @@ -6,6 +6,7 @@ import dweepy import voluptuous as vol from homeassistant.const import ( + ATTR_FRIENDLY_NAME, CONF_NAME, CONF_WHITELIST, EVENT_STATE_CHANGED, @@ -58,7 +59,7 @@ def setup(hass, config): except ValueError: _state = state.state - json_body[state.attributes.get("friendly_name")] = _state + json_body[state.attributes.get(ATTR_FRIENDLY_NAME)] = _state send_data(name, json_body) diff --git a/homeassistant/components/homekit/type_security_systems.py b/homeassistant/components/homekit/type_security_systems.py index a5530a45d56..feae1b5cd06 100644 --- a/homeassistant/components/homekit/type_security_systems.py +++ b/homeassistant/components/homekit/type_security_systems.py @@ -14,6 +14,7 @@ from homeassistant.components.alarm_control_panel.const import ( from homeassistant.const import ( ATTR_CODE, ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, SERVICE_ALARM_ARM_AWAY, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_ARM_NIGHT, @@ -71,7 +72,7 @@ class SecuritySystem(HomeAccessory): self._alarm_code = self.config.get(ATTR_CODE) supported_states = state.attributes.get( - "supported_features", + ATTR_SUPPORTED_FEATURES, ( SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY diff --git a/homeassistant/components/humidifier/device_action.py b/homeassistant/components/humidifier/device_action.py index a6194994a9c..6bccd375207 100644 --- a/homeassistant/components/humidifier/device_action.py +++ b/homeassistant/components/humidifier/device_action.py @@ -6,6 +6,7 @@ import voluptuous as vol from homeassistant.components.device_automation import toggle_entity from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_DEVICE_ID, CONF_DOMAIN, CONF_ENTITY_ID, @@ -63,7 +64,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]: if state is None: continue - if state.attributes["supported_features"] & const.SUPPORT_MODES: + if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_MODES: actions.append( { CONF_DEVICE_ID: device_id, diff --git a/homeassistant/components/humidifier/device_condition.py b/homeassistant/components/humidifier/device_condition.py index 7f37fc3b1fa..714a51ab016 100644 --- a/homeassistant/components/humidifier/device_condition.py +++ b/homeassistant/components/humidifier/device_condition.py @@ -6,6 +6,7 @@ import voluptuous as vol from homeassistant.components.device_automation import toggle_entity from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, CONF_CONDITION, CONF_DEVICE_ID, CONF_DOMAIN, @@ -48,7 +49,7 @@ async def async_get_conditions( state = hass.states.get(entry.entity_id) - if state and state.attributes["supported_features"] & const.SUPPORT_MODES: + if state and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_MODES: conditions.append( { CONF_CONDITION: "device", diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index 2f5e69fd02b..bbeea2e9521 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -26,6 +26,7 @@ from homeassistant.const import ( ATTR_DOMAIN, ATTR_SERVICE, ATTR_SERVICE_DATA, + ATTR_SUPPORTED_FEATURES, CONF_WEBHOOK_ID, HTTP_BAD_REQUEST, HTTP_CREATED, @@ -267,7 +268,7 @@ async def webhook_stream_camera(hass, config_entry, data): resp = {"mjpeg_path": "/api/camera_proxy_stream/%s" % (camera.entity_id)} - if camera.attributes["supported_features"] & CAMERA_SUPPORT_STREAM: + if camera.attributes[ATTR_SUPPORTED_FEATURES] & CAMERA_SUPPORT_STREAM: try: resp["hls_path"] = await hass.components.camera.async_request_stream( camera.entity_id, "hls" diff --git a/homeassistant/components/owntracks/__init__.py b/homeassistant/components/owntracks/__init__.py index 24dc99de71c..d3091d7d027 100644 --- a/homeassistant/components/owntracks/__init__.py +++ b/homeassistant/components/owntracks/__init__.py @@ -9,7 +9,12 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.components import mqtt -from homeassistant.const import CONF_WEBHOOK_ID +from homeassistant.const import ( + ATTR_GPS_ACCURACY, + ATTR_LATITUDE, + ATTR_LONGITUDE, + CONF_WEBHOOK_ID, +) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.setup import async_when_setup @@ -292,9 +297,9 @@ class OwnTracksContext: device_tracker_state = hass.states.get(f"device_tracker.{dev_id}") if device_tracker_state is not None: - acc = device_tracker_state.attributes.get("gps_accuracy") - lat = device_tracker_state.attributes.get("latitude") - lon = device_tracker_state.attributes.get("longitude") + acc = device_tracker_state.attributes.get(ATTR_GPS_ACCURACY) + lat = device_tracker_state.attributes.get(ATTR_LATITUDE) + lon = device_tracker_state.attributes.get(ATTR_LONGITUDE) if lat is not None and lon is not None: kwargs["gps"] = (lat, lon) diff --git a/homeassistant/components/owntracks/messages.py b/homeassistant/components/owntracks/messages.py index 5e610d861fe..3a4aac6bfd1 100644 --- a/homeassistant/components/owntracks/messages.py +++ b/homeassistant/components/owntracks/messages.py @@ -10,7 +10,7 @@ from homeassistant.components.device_tracker import ( SOURCE_TYPE_BLUETOOTH_LE, SOURCE_TYPE_GPS, ) -from homeassistant.const import STATE_HOME +from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME from homeassistant.util import decorator, slugify from .helper import supports_encryption @@ -97,7 +97,10 @@ def _set_gps_from_zone(kwargs, location, zone): Async friendly. """ if zone is not None: - kwargs["gps"] = (zone.attributes["latitude"], zone.attributes["longitude"]) + kwargs["gps"] = ( + zone.attributes[ATTR_LATITUDE], + zone.attributes[ATTR_LONGITUDE], + ) kwargs["gps_accuracy"] = zone.attributes["radius"] kwargs["location_name"] = location return kwargs diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 87a8a2c41f3..bd9a6e35276 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -19,7 +19,9 @@ from homeassistant.components.humidifier.const import ( ATTR_MODE, ) from homeassistant.const import ( + ATTR_BATTERY_LEVEL, ATTR_DEVICE_CLASS, + ATTR_FRIENDLY_NAME, ATTR_TEMPERATURE, ATTR_UNIT_OF_MEASUREMENT, CONTENT_TYPE_TEXT_PLAIN, @@ -234,7 +236,7 @@ class PrometheusMetrics: return { "entity": state.entity_id, "domain": state.domain, - "friendly_name": state.attributes.get("friendly_name"), + "friendly_name": state.attributes.get(ATTR_FRIENDLY_NAME), } def _battery(self, state): @@ -245,7 +247,7 @@ class PrometheusMetrics: "Battery level as a percentage of its capacity", ) try: - value = float(state.attributes["battery_level"]) + value = float(state.attributes[ATTR_BATTERY_LEVEL]) metric.labels(**self._labels(state)).set(value) except ValueError: pass diff --git a/homeassistant/components/proximity/__init__.py b/homeassistant/components/proximity/__init__.py index 7beaaaf00e1..2d0d14a69c1 100644 --- a/homeassistant/components/proximity/__init__.py +++ b/homeassistant/components/proximity/__init__.py @@ -4,6 +4,8 @@ import logging import voluptuous as vol from homeassistant.const import ( + ATTR_LATITUDE, + ATTR_LONGITUDE, CONF_DEVICES, CONF_UNIT_OF_MEASUREMENT, CONF_ZONE, @@ -149,8 +151,8 @@ class Proximity(Entity): devices_in_zone = "" zone_state = self.hass.states.get(self.proximity_zone) - proximity_latitude = zone_state.attributes.get("latitude") - proximity_longitude = zone_state.attributes.get("longitude") + proximity_latitude = zone_state.attributes.get(ATTR_LATITUDE) + proximity_longitude = zone_state.attributes.get(ATTR_LONGITUDE) # Check for devices in the monitored zone. for device in self.proximity_devices: @@ -206,8 +208,8 @@ class Proximity(Entity): dist_to_zone = distance( proximity_latitude, proximity_longitude, - device_state.attributes["latitude"], - device_state.attributes["longitude"], + device_state.attributes[ATTR_LATITUDE], + device_state.attributes[ATTR_LONGITUDE], ) # Add the device and distance to a dictionary. @@ -250,14 +252,14 @@ class Proximity(Entity): old_distance = distance( proximity_latitude, proximity_longitude, - old_state.attributes["latitude"], - old_state.attributes["longitude"], + old_state.attributes[ATTR_LATITUDE], + old_state.attributes[ATTR_LONGITUDE], ) new_distance = distance( proximity_latitude, proximity_longitude, - new_state.attributes["latitude"], - new_state.attributes["longitude"], + new_state.attributes[ATTR_LATITUDE], + new_state.attributes[ATTR_LONGITUDE], ) distance_travelled = round(new_distance - old_distance, 1) diff --git a/tests/components/august/test_sensor.py b/tests/components/august/test_sensor.py index 7e69b59da07..51e00b9d09f 100644 --- a/tests/components/august/test_sensor.py +++ b/tests/components/august/test_sensor.py @@ -1,6 +1,6 @@ """The sensor tests for the august platform.""" -from homeassistant.const import PERCENTAGE, STATE_UNAVAILABLE +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, STATE_UNAVAILABLE from tests.components.august.mocks import ( _create_august_with_devices, @@ -75,7 +75,7 @@ async def test_create_lock_with_linked_keypad(hass): state = hass.states.get("sensor.front_door_lock_keypad_battery") assert state.state == "60" - assert state.attributes["unit_of_measurement"] == PERCENTAGE + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") assert entry assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" @@ -105,7 +105,7 @@ async def test_create_lock_with_low_battery_linked_keypad(hass): state = hass.states.get("sensor.front_door_lock_keypad_battery") assert state.state == "10" - assert state.attributes["unit_of_measurement"] == PERCENTAGE + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") assert entry assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" diff --git a/tests/components/config/test_entity_registry.py b/tests/components/config/test_entity_registry.py index 2a696624e0c..d63d10437cc 100644 --- a/tests/components/config/test_entity_registry.py +++ b/tests/components/config/test_entity_registry.py @@ -4,6 +4,7 @@ from collections import OrderedDict import pytest from homeassistant.components.config import entity_registry +from homeassistant.const import ATTR_ICON from homeassistant.helpers.entity_registry import RegistryEntry from tests.common import MockEntity, MockEntityPlatform, mock_registry @@ -140,7 +141,7 @@ async def test_update_entity(hass, client): state = hass.states.get("test_domain.world") assert state is not None assert state.name == "before update" - assert state.attributes["icon"] == "icon:before update" + assert state.attributes[ATTR_ICON] == "icon:before update" # UPDATE NAME & ICON await client.send_json( @@ -171,7 +172,7 @@ async def test_update_entity(hass, client): state = hass.states.get("test_domain.world") assert state.name == "after update" - assert state.attributes["icon"] == "icon:after update" + assert state.attributes[ATTR_ICON] == "icon:after update" # UPDATE DISABLED_BY TO USER await client.send_json( diff --git a/tests/components/demo/test_geo_location.py b/tests/components/demo/test_geo_location.py index 7ca870bc34f..ac32fff075f 100644 --- a/tests/components/demo/test_geo_location.py +++ b/tests/components/demo/test_geo_location.py @@ -8,7 +8,12 @@ from homeassistant.components.demo.geo_location import ( DEFAULT_UPDATE_INTERVAL, NUMBER_OF_DEMO_DEVICES, ) -from homeassistant.const import LENGTH_KILOMETERS +from homeassistant.const import ( + ATTR_LATITUDE, + ATTR_LONGITUDE, + ATTR_UNIT_OF_MEASUREMENT, + LENGTH_KILOMETERS, +) from homeassistant.setup import setup_component import homeassistant.util.dt as dt_util @@ -59,13 +64,14 @@ class TestDemoPlatform(unittest.TestCase): # ignore home zone state continue assert ( - abs(state.attributes["latitude"] - self.hass.config.latitude) < 1.0 - ) - assert ( - abs(state.attributes["longitude"] - self.hass.config.longitude) + abs(state.attributes[ATTR_LATITUDE] - self.hass.config.latitude) < 1.0 ) - assert state.attributes["unit_of_measurement"] == LENGTH_KILOMETERS + assert ( + abs(state.attributes[ATTR_LONGITUDE] - self.hass.config.longitude) + < 1.0 + ) + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == LENGTH_KILOMETERS # Update (replaces 1 device). fire_time_changed(self.hass, utcnow + DEFAULT_UPDATE_INTERVAL) diff --git a/tests/components/demo/test_media_player.py b/tests/components/demo/test_media_player.py index 1ab8195c4db..70b05cd25cc 100644 --- a/tests/components/demo/test_media_player.py +++ b/tests/components/demo/test_media_player.py @@ -3,6 +3,7 @@ import pytest import voluptuous as vol import homeassistant.components.media_player as mp +from homeassistant.const import ATTR_SUPPORTED_FEATURES from homeassistant.helpers.aiohttp_client import DATA_CLIENTSESSION from homeassistant.setup import async_setup_component @@ -203,7 +204,7 @@ async def test_seek(hass, mock_media_seek): await hass.async_block_till_done() ent_id = "media_player.living_room" state = hass.states.get(ent_id) - assert state.attributes["supported_features"] & mp.SUPPORT_SEEK + assert state.attributes[ATTR_SUPPORTED_FEATURES] & mp.SUPPORT_SEEK assert not mock_media_seek.called with pytest.raises(vol.Invalid): await common.async_media_seek(hass, None, ent_id) diff --git a/tests/components/dynalite/test_cover.py b/tests/components/dynalite/test_cover.py index cef4081c607..4f696d905d3 100644 --- a/tests/components/dynalite/test_cover.py +++ b/tests/components/dynalite/test_cover.py @@ -2,6 +2,8 @@ from dynalite_devices_lib.cover import DynaliteTimeCoverWithTiltDevice import pytest +from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_FRIENDLY_NAME + from .common import ( ATTR_ARGS, ATTR_METHOD, @@ -24,7 +26,7 @@ async def test_cover_setup(hass, mock_device): """Test a successful setup.""" await create_entity_from_device(hass, mock_device) entity_state = hass.states.get("cover.name") - assert entity_state.attributes["friendly_name"] == mock_device.name + assert entity_state.attributes[ATTR_FRIENDLY_NAME] == mock_device.name assert ( entity_state.attributes["current_position"] == mock_device.current_cover_position @@ -33,7 +35,7 @@ async def test_cover_setup(hass, mock_device): entity_state.attributes["current_tilt_position"] == mock_device.current_cover_tilt_position ) - assert entity_state.attributes["device_class"] == mock_device.device_class + assert entity_state.attributes[ATTR_DEVICE_CLASS] == mock_device.device_class await run_service_tests( hass, mock_device, diff --git a/tests/components/dynalite/test_light.py b/tests/components/dynalite/test_light.py index deea32d2e34..7df10fb08e8 100644 --- a/tests/components/dynalite/test_light.py +++ b/tests/components/dynalite/test_light.py @@ -4,6 +4,7 @@ from dynalite_devices_lib.light import DynaliteChannelLightDevice import pytest from homeassistant.components.light import SUPPORT_BRIGHTNESS +from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_SUPPORTED_FEATURES from .common import ( ATTR_METHOD, @@ -25,9 +26,9 @@ async def test_light_setup(hass, mock_device): """Test a successful setup.""" await create_entity_from_device(hass, mock_device) entity_state = hass.states.get("light.name") - assert entity_state.attributes["friendly_name"] == mock_device.name + assert entity_state.attributes[ATTR_FRIENDLY_NAME] == mock_device.name assert entity_state.attributes["brightness"] == mock_device.brightness - assert entity_state.attributes["supported_features"] == SUPPORT_BRIGHTNESS + assert entity_state.attributes[ATTR_SUPPORTED_FEATURES] == SUPPORT_BRIGHTNESS await run_service_tests( hass, mock_device, diff --git a/tests/components/dynalite/test_switch.py b/tests/components/dynalite/test_switch.py index 7c0c5d632d3..de375e3b348 100644 --- a/tests/components/dynalite/test_switch.py +++ b/tests/components/dynalite/test_switch.py @@ -3,6 +3,8 @@ from dynalite_devices_lib.switch import DynalitePresetSwitchDevice import pytest +from homeassistant.const import ATTR_FRIENDLY_NAME + from .common import ( ATTR_METHOD, ATTR_SERVICE, @@ -22,7 +24,7 @@ async def test_switch_setup(hass, mock_device): """Test a successful setup.""" await create_entity_from_device(hass, mock_device) entity_state = hass.states.get("switch.name") - assert entity_state.attributes["friendly_name"] == mock_device.name + assert entity_state.attributes[ATTR_FRIENDLY_NAME] == mock_device.name await run_service_tests( hass, mock_device, diff --git a/tests/components/eafm/test_sensor.py b/tests/components/eafm/test_sensor.py index 6cce7a2bc4b..a7ee0403c7c 100644 --- a/tests/components/eafm/test_sensor.py +++ b/tests/components/eafm/test_sensor.py @@ -5,6 +5,7 @@ import aiohttp import pytest from homeassistant import config_entries +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -250,7 +251,7 @@ async def test_reading_is_sampled(hass, mock_get_station): state = hass.states.get("sensor.my_station_water_level_stage") assert state.state == "5" - assert state.attributes["unit_of_measurement"] == "m" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "m" async def test_multiple_readings_are_sampled(hass, mock_get_station): @@ -287,11 +288,11 @@ async def test_multiple_readings_are_sampled(hass, mock_get_station): state = hass.states.get("sensor.my_station_water_level_stage") assert state.state == "5" - assert state.attributes["unit_of_measurement"] == "m" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "m" state = hass.states.get("sensor.my_station_water_level_second_stage") assert state.state == "4" - assert state.attributes["unit_of_measurement"] == "m" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "m" async def test_ignore_no_latest_reading(hass, mock_get_station): @@ -327,7 +328,7 @@ async def test_ignore_no_latest_reading(hass, mock_get_station): state = hass.states.get("sensor.my_station_water_level_stage") assert state.state == "5" - assert state.attributes["unit_of_measurement"] == "m" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "m" state = hass.states.get("sensor.my_station_water_level_second_stage") assert state is None @@ -357,7 +358,7 @@ async def test_mark_existing_as_unavailable_if_no_latest(hass, mock_get_station) state = hass.states.get("sensor.my_station_water_level_stage") assert state.state == "5" - assert state.attributes["unit_of_measurement"] == "m" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "m" await poll( { diff --git a/tests/components/geofency/test_init.py b/tests/components/geofency/test_init.py index dd3132b0117..21b6830e7f4 100644 --- a/tests/components/geofency/test_init.py +++ b/tests/components/geofency/test_init.py @@ -6,6 +6,8 @@ from homeassistant.components import zone from homeassistant.components.geofency import CONF_MOBILE_BEACONS, DOMAIN from homeassistant.config import async_process_ha_core_config from homeassistant.const import ( + ATTR_LATITUDE, + ATTR_LONGITUDE, HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, STATE_HOME, @@ -316,5 +318,5 @@ async def test_load_unload_entry(hass, geofency_client, webhook_id): assert state_1 is not state_2 assert STATE_HOME == state_2.state - assert state_2.attributes["latitude"] == HOME_LATITUDE - assert state_2.attributes["longitude"] == HOME_LONGITUDE + assert state_2.attributes[ATTR_LATITUDE] == HOME_LATITUDE + assert state_2.attributes[ATTR_LONGITUDE] == HOME_LONGITUDE diff --git a/tests/components/rflink/test_sensor.py b/tests/components/rflink/test_sensor.py index 1468037b70d..d18076a372a 100644 --- a/tests/components/rflink/test_sensor.py +++ b/tests/components/rflink/test_sensor.py @@ -12,7 +12,12 @@ from homeassistant.components.rflink import ( EVENT_KEY_SENSOR, TMP_ENTITY, ) -from homeassistant.const import PERCENTAGE, STATE_UNKNOWN, TEMP_CELSIUS +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + PERCENTAGE, + STATE_UNKNOWN, + TEMP_CELSIUS, +) from tests.components.rflink.test_init import mock_rflink @@ -42,7 +47,7 @@ async def test_default_setup(hass, monkeypatch): config_sensor = hass.states.get("sensor.test") assert config_sensor assert config_sensor.state == "unknown" - assert config_sensor.attributes["unit_of_measurement"] == TEMP_CELSIUS + assert config_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS # test event for config sensor event_callback( @@ -62,7 +67,7 @@ async def test_default_setup(hass, monkeypatch): new_sensor = hass.states.get("sensor.test2") assert new_sensor assert new_sensor.state == "0" - assert new_sensor.attributes["unit_of_measurement"] == TEMP_CELSIUS + assert new_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS assert new_sensor.attributes["icon"] == "mdi:thermometer" @@ -160,7 +165,7 @@ async def test_aliases(hass, monkeypatch): updated_sensor = hass.states.get("sensor.test_02") assert updated_sensor assert updated_sensor.state == "65" - assert updated_sensor.attributes["unit_of_measurement"] == PERCENTAGE + assert updated_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE async def test_race_condition(hass, monkeypatch): diff --git a/tests/components/rfxtrx/test_sensor.py b/tests/components/rfxtrx/test_sensor.py index 18239550a85..0186d403245 100644 --- a/tests/components/rfxtrx/test_sensor.py +++ b/tests/components/rfxtrx/test_sensor.py @@ -2,7 +2,7 @@ import pytest from homeassistant.components.rfxtrx.const import ATTR_EVENT -from homeassistant.const import PERCENTAGE, TEMP_CELSIUS +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, TEMP_CELSIUS from homeassistant.core import State from homeassistant.setup import async_setup_component @@ -35,7 +35,7 @@ async def test_one_sensor(hass, rfxtrx): state.attributes.get("friendly_name") == "WT260,WT260H,WT440H,WT450,WT450H 05:02 Temperature" ) - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS @pytest.mark.parametrize( @@ -75,31 +75,31 @@ async def test_one_sensor_no_datatype(hass, rfxtrx): assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Temperature" - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get(f"{base_id}_humidity") assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Humidity" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Humidity status" - assert state.attributes.get("unit_of_measurement") == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" state = hass.states.get(f"{base_id}_rssi_numeric") assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Rssi numeric" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" state = hass.states.get(f"{base_id}_battery_numeric") assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == f"{base_name} Battery numeric" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE async def test_several_sensors(hass, rfxtrx): @@ -127,7 +127,7 @@ async def test_several_sensors(hass, rfxtrx): state.attributes.get("friendly_name") == "WT260,WT260H,WT440H,WT450,WT450H 05:02 Temperature" ) - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature") assert state @@ -136,7 +136,7 @@ async def test_several_sensors(hass, rfxtrx): state.attributes.get("friendly_name") == "WT260,WT260H,WT440H,WT450,WT450H 06:01 Temperature" ) - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_humidity") assert state @@ -145,7 +145,7 @@ async def test_several_sensors(hass, rfxtrx): state.attributes.get("friendly_name") == "WT260,WT260H,WT440H,WT450,WT450H 06:01 Humidity" ) - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE async def test_discover_sensor(hass, rfxtrx_automatic): @@ -159,27 +159,27 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_humidity") assert state assert state.state == "27" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get("unit_of_measurement") == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" state = hass.states.get(f"{base_id}_rssi_numeric") assert state assert state.state == "-64" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" state = hass.states.get(f"{base_id}_temperature") assert state assert state.state == "18.4" - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get(f"{base_id}_battery_numeric") assert state assert state.state == "90" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE # 2 await rfxtrx.signal("0a52080405020095240279") @@ -188,27 +188,27 @@ async def test_discover_sensor(hass, rfxtrx_automatic): assert state assert state.state == "36" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get("unit_of_measurement") == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" state = hass.states.get(f"{base_id}_rssi_numeric") assert state assert state.state == "-64" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" state = hass.states.get(f"{base_id}_temperature") assert state assert state.state == "14.9" - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get(f"{base_id}_battery_numeric") assert state assert state.state == "90" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE # 1 Update await rfxtrx.signal("0a52085e070100b31b0279") @@ -217,27 +217,27 @@ async def test_discover_sensor(hass, rfxtrx_automatic): state = hass.states.get(f"{base_id}_humidity") assert state assert state.state == "27" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE state = hass.states.get(f"{base_id}_humidity_status") assert state assert state.state == "normal" - assert state.attributes.get("unit_of_measurement") == "" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" state = hass.states.get(f"{base_id}_rssi_numeric") assert state assert state.state == "-64" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" state = hass.states.get(f"{base_id}_temperature") assert state assert state.state == "17.9" - assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS state = hass.states.get(f"{base_id}_battery_numeric") assert state assert state.state == "90" - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert len(hass.states.async_all()) == 10 @@ -314,13 +314,13 @@ async def test_rssi_sensor(hass, rfxtrx): assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == "PT2262 22670e Rssi numeric" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" state = hass.states.get("sensor.ac_213c7f2_48_rssi_numeric") assert state assert state.state == "unknown" assert state.attributes.get("friendly_name") == "AC 213c7f2:48 Rssi numeric" - assert state.attributes.get("unit_of_measurement") == "dBm" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "dBm" await rfxtrx.signal("0913000022670e013b70") await rfxtrx.signal("0b1100cd0213c7f230010f71") diff --git a/tests/components/sma/test_sensor.py b/tests/components/sma/test_sensor.py index 2c317520a7d..f12af1f3849 100644 --- a/tests/components/sma/test_sensor.py +++ b/tests/components/sma/test_sensor.py @@ -2,7 +2,7 @@ import logging from homeassistant.components.sensor import DOMAIN -from homeassistant.const import VOLT +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, VOLT from homeassistant.setup import async_setup_component from tests.common import assert_setup_component @@ -28,7 +28,7 @@ async def test_sma_config(hass): state = hass.states.get("sensor.current_consumption") assert state - assert "unit_of_measurement" in state.attributes + assert ATTR_UNIT_OF_MEASUREMENT in state.attributes assert "current_consumption" not in state.attributes state = hass.states.get("sensor.my_sensor") diff --git a/tests/components/spaceapi/test_init.py b/tests/components/spaceapi/test_init.py index b831de56b05..0f33434254c 100644 --- a/tests/components/spaceapi/test_init.py +++ b/tests/components/spaceapi/test_init.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest from homeassistant.components.spaceapi import DOMAIN, SPACEAPI_VERSION, URL_API_SPACEAPI -from homeassistant.const import PERCENTAGE, TEMP_CELSIUS +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, TEMP_CELSIUS from homeassistant.setup import async_setup_component from tests.common import mock_coro @@ -76,13 +76,13 @@ def mock_client(hass, hass_client): hass.loop.run_until_complete(async_setup_component(hass, "spaceapi", CONFIG)) hass.states.async_set( - "test.temp1", 25, attributes={"unit_of_measurement": TEMP_CELSIUS} + "test.temp1", 25, attributes={ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} ) hass.states.async_set( - "test.temp2", 23, attributes={"unit_of_measurement": TEMP_CELSIUS} + "test.temp2", 23, attributes={ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} ) hass.states.async_set( - "test.hum1", 88, attributes={"unit_of_measurement": PERCENTAGE} + "test.hum1", 88, attributes={ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE} ) return hass.loop.run_until_complete(hass_client()) diff --git a/tests/components/startca/test_sensor.py b/tests/components/startca/test_sensor.py index e1d658d05b7..511061933cb 100644 --- a/tests/components/startca/test_sensor.py +++ b/tests/components/startca/test_sensor.py @@ -1,7 +1,12 @@ """Tests for the Start.ca sensor platform.""" from homeassistant.bootstrap import async_setup_component from homeassistant.components.startca.sensor import StartcaData -from homeassistant.const import DATA_GIGABYTES, HTTP_NOT_FOUND, PERCENTAGE +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + DATA_GIGABYTES, + HTTP_NOT_FOUND, + PERCENTAGE, +) from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -53,51 +58,51 @@ async def test_capped_setup(hass, aioclient_mock): await hass.async_block_till_done() state = hass.states.get("sensor.start_ca_usage_ratio") - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert state.state == "76.24" state = hass.states.get("sensor.start_ca_usage") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_data_limit") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "400" state = hass.states.get("sensor.start_ca_used_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_used_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "6.48" state = hass.states.get("sensor.start_ca_used_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "311.43" state = hass.states.get("sensor.start_ca_grace_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_grace_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "6.48" state = hass.states.get("sensor.start_ca_grace_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "311.43" state = hass.states.get("sensor.start_ca_total_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_total_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "6.48" state = hass.states.get("sensor.start_ca_remaining") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "95.05" @@ -149,51 +154,51 @@ async def test_unlimited_setup(hass, aioclient_mock): await hass.async_block_till_done() state = hass.states.get("sensor.start_ca_usage_ratio") - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert state.state == "0" state = hass.states.get("sensor.start_ca_usage") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "0.0" state = hass.states.get("sensor.start_ca_data_limit") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "inf" state = hass.states.get("sensor.start_ca_used_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "0.0" state = hass.states.get("sensor.start_ca_used_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "0.0" state = hass.states.get("sensor.start_ca_used_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "0.0" state = hass.states.get("sensor.start_ca_grace_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_grace_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "6.48" state = hass.states.get("sensor.start_ca_grace_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "311.43" state = hass.states.get("sensor.start_ca_total_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "304.95" state = hass.states.get("sensor.start_ca_total_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "6.48" state = hass.states.get("sensor.start_ca_remaining") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "inf" diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index e60c5c2e9a5..24401963974 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -115,7 +115,7 @@ class TestStatisticsSensor(unittest.TestCase): assert self.mean == state.attributes.get("mean") assert self.count == state.attributes.get("count") assert self.total == state.attributes.get("total") - assert TEMP_CELSIUS == state.attributes.get("unit_of_measurement") + assert TEMP_CELSIUS == state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) assert self.change == state.attributes.get("change") assert self.average_change == state.attributes.get("average_change") diff --git a/tests/components/teksavvy/test_sensor.py b/tests/components/teksavvy/test_sensor.py index b0de95d72d1..aa20beeba8b 100644 --- a/tests/components/teksavvy/test_sensor.py +++ b/tests/components/teksavvy/test_sensor.py @@ -1,7 +1,12 @@ """Tests for the TekSavvy sensor platform.""" from homeassistant.bootstrap import async_setup_component from homeassistant.components.teksavvy.sensor import TekSavvyData -from homeassistant.const import DATA_GIGABYTES, HTTP_NOT_FOUND, PERCENTAGE +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + DATA_GIGABYTES, + HTTP_NOT_FOUND, + PERCENTAGE, +) from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -47,43 +52,43 @@ async def test_capped_setup(hass, aioclient_mock): await hass.async_block_till_done() state = hass.states.get("sensor.teksavvy_data_limit") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "400" state = hass.states.get("sensor.teksavvy_off_peak_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "36.24" state = hass.states.get("sensor.teksavvy_off_peak_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "1.58" state = hass.states.get("sensor.teksavvy_off_peak_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "37.82" state = hass.states.get("sensor.teksavvy_on_peak_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "226.75" state = hass.states.get("sensor.teksavvy_on_peak_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "8.82" state = hass.states.get("sensor.teksavvy_on_peak_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "235.57" state = hass.states.get("sensor.teksavvy_usage_ratio") - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert state.state == "56.69" state = hass.states.get("sensor.teksavvy_usage") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "226.75" state = hass.states.get("sensor.teksavvy_remaining") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "173.25" @@ -129,43 +134,43 @@ async def test_unlimited_setup(hass, aioclient_mock): await hass.async_block_till_done() state = hass.states.get("sensor.teksavvy_data_limit") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "inf" state = hass.states.get("sensor.teksavvy_off_peak_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "36.24" state = hass.states.get("sensor.teksavvy_off_peak_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "1.58" state = hass.states.get("sensor.teksavvy_off_peak_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "37.82" state = hass.states.get("sensor.teksavvy_on_peak_download") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "226.75" state = hass.states.get("sensor.teksavvy_on_peak_upload") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "8.82" state = hass.states.get("sensor.teksavvy_on_peak_total") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "235.57" state = hass.states.get("sensor.teksavvy_usage") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "226.75" state = hass.states.get("sensor.teksavvy_usage_ratio") - assert state.attributes.get("unit_of_measurement") == PERCENTAGE + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert state.state == "0" state = hass.states.get("sensor.teksavvy_remaining") - assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES assert state.state == "inf" diff --git a/tests/components/utility_meter/test_init.py b/tests/components/utility_meter/test_init.py index 7116077177a..f946ce352a5 100644 --- a/tests/components/utility_meter/test_init.py +++ b/tests/components/utility_meter/test_init.py @@ -12,6 +12,7 @@ from homeassistant.components.utility_meter.const import ( ) from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_UNIT_OF_MEASUREMENT, ENERGY_KILO_WATT_HOUR, EVENT_HOMEASSISTANT_START, ) @@ -41,7 +42,9 @@ async def test_services(hass): hass.bus.async_fire(EVENT_HOMEASSISTANT_START) entity_id = config[DOMAIN]["energy_bill"]["source"] - hass.states.async_set(entity_id, 1, {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}) + hass.states.async_set( + entity_id, 1, {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR} + ) await hass.async_block_till_done() now = dt_util.utcnow() + timedelta(seconds=10) @@ -49,7 +52,7 @@ async def test_services(hass): hass.states.async_set( entity_id, 3, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -70,7 +73,7 @@ async def test_services(hass): hass.states.async_set( entity_id, 4, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -91,7 +94,7 @@ async def test_services(hass): hass.states.async_set( entity_id, 5, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() diff --git a/tests/components/utility_meter/test_sensor.py b/tests/components/utility_meter/test_sensor.py index af2bbdbf6a2..1fff168b748 100644 --- a/tests/components/utility_meter/test_sensor.py +++ b/tests/components/utility_meter/test_sensor.py @@ -13,6 +13,7 @@ from homeassistant.components.utility_meter.const import ( ) from homeassistant.const import ( ATTR_ENTITY_ID, + ATTR_UNIT_OF_MEASUREMENT, ENERGY_KILO_WATT_HOUR, EVENT_HOMEASSISTANT_START, ) @@ -52,7 +53,9 @@ async def test_state(hass): hass.bus.async_fire(EVENT_HOMEASSISTANT_START) entity_id = config[DOMAIN]["energy_bill"]["source"] - hass.states.async_set(entity_id, 2, {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}) + hass.states.async_set( + entity_id, 2, {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR} + ) await hass.async_block_till_done() now = dt_util.utcnow() + timedelta(seconds=10) @@ -60,7 +63,7 @@ async def test_state(hass): hass.states.async_set( entity_id, 3, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -91,7 +94,7 @@ async def test_state(hass): hass.states.async_set( entity_id, 6, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -145,7 +148,9 @@ async def test_net_consumption(hass): hass.bus.async_fire(EVENT_HOMEASSISTANT_START) entity_id = config[DOMAIN]["energy_bill"]["source"] - hass.states.async_set(entity_id, 2, {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}) + hass.states.async_set( + entity_id, 2, {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR} + ) await hass.async_block_till_done() now = dt_util.utcnow() + timedelta(seconds=10) @@ -153,7 +158,7 @@ async def test_net_consumption(hass): hass.states.async_set( entity_id, 1, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -178,7 +183,9 @@ async def test_non_net_consumption(hass): hass.bus.async_fire(EVENT_HOMEASSISTANT_START) entity_id = config[DOMAIN]["energy_bill"]["source"] - hass.states.async_set(entity_id, 2, {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}) + hass.states.async_set( + entity_id, 2, {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR} + ) await hass.async_block_till_done() now = dt_util.utcnow() + timedelta(seconds=10) @@ -186,7 +193,7 @@ async def test_non_net_consumption(hass): hass.states.async_set( entity_id, 1, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -224,7 +231,7 @@ async def _test_self_reset(hass, config, start_time, expect_reset=True): with alter_time(now): async_fire_time_changed(hass, now) hass.states.async_set( - entity_id, 1, {"unit_of_measurement": ENERGY_KILO_WATT_HOUR} + entity_id, 1, {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR} ) await hass.async_block_till_done() @@ -234,7 +241,7 @@ async def _test_self_reset(hass, config, start_time, expect_reset=True): hass.states.async_set( entity_id, 3, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() @@ -246,7 +253,7 @@ async def _test_self_reset(hass, config, start_time, expect_reset=True): hass.states.async_set( entity_id, 6, - {"unit_of_measurement": ENERGY_KILO_WATT_HOUR}, + {ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR}, force_update=True, ) await hass.async_block_till_done() diff --git a/tests/components/vera/test_sensor.py b/tests/components/vera/test_sensor.py index 58e91a5581b..58cedeee450 100644 --- a/tests/components/vera/test_sensor.py +++ b/tests/components/vera/test_sensor.py @@ -3,7 +3,7 @@ from typing import Any, Callable, Tuple import pyvera as pv -from homeassistant.const import PERCENTAGE +from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE from homeassistant.core import HomeAssistant from .common import ComponentFactory, new_simple_controller_config @@ -44,7 +44,9 @@ async def run_sensor_test( state = hass.states.get(entity_id) assert state.state == state_value if assert_unit_of_measurement: - assert state.attributes["unit_of_measurement"] == assert_unit_of_measurement + assert ( + state.attributes[ATTR_UNIT_OF_MEASUREMENT] == assert_unit_of_measurement + ) async def test_temperature_sensor_f( diff --git a/tests/components/wunderground/test_sensor.py b/tests/components/wunderground/test_sensor.py index b4fb30d25c5..8709f5b6a46 100644 --- a/tests/components/wunderground/test_sensor.py +++ b/tests/components/wunderground/test_sensor.py @@ -3,7 +3,12 @@ import aiohttp from pytest import raises import homeassistant.components.wunderground.sensor as wunderground -from homeassistant.const import LENGTH_INCHES, STATE_UNKNOWN, TEMP_CELSIUS +from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, + LENGTH_INCHES, + STATE_UNKNOWN, + TEMP_CELSIUS, +) from homeassistant.exceptions import PlatformNotReady from homeassistant.setup import async_setup_component @@ -90,7 +95,7 @@ async def test_sensor(hass, aioclient_mock): state = hass.states.get("sensor.pws_weather") assert state.state == "Clear" assert state.name == "Weather Summary" - assert "unit_of_measurement" not in state.attributes + assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes assert ( state.attributes["entity_picture"] == "https://icons.wxug.com/i/c/k/clear.gif" ) @@ -114,7 +119,7 @@ async def test_sensor(hass, aioclient_mock): assert state.state == "40" assert state.name == "Feels Like" assert "entity_picture" not in state.attributes - assert state.attributes["unit_of_measurement"] == TEMP_CELSIUS + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS state = hass.states.get("sensor.pws_weather_1d_metric") assert state.state == "Mostly Cloudy. Fog overnight." @@ -123,7 +128,7 @@ async def test_sensor(hass, aioclient_mock): state = hass.states.get("sensor.pws_precip_1d_in") assert state.state == "0.03" assert state.name == "Precipitation Intensity Today" - assert state.attributes["unit_of_measurement"] == LENGTH_INCHES + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == LENGTH_INCHES async def test_connect_failed(hass, aioclient_mock): diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 81deb46f928..ca36d8612d4 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -8,6 +8,7 @@ import pytz from homeassistant.components import group from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, LENGTH_METERS, MASS_GRAMS, MATCH_ALL, @@ -2271,7 +2272,7 @@ def test_jinja_namespace(hass): def test_state_with_unit(hass): """Test the state_with_unit property helper.""" - hass.states.async_set("sensor.test", "23", {"unit_of_measurement": "beers"}) + hass.states.async_set("sensor.test", "23", {ATTR_UNIT_OF_MEASUREMENT: "beers"}) hass.states.async_set("sensor.test2", "wow") tpl = template.Template("{{ states.sensor.test.state_with_unit }}", hass)