Various string cleanups (#30435)

* Remove some unnecessary string concatenations

* Replace some simple str.formats with f-strings

* Replace some string concatenations with f-strings
This commit is contained in:
Ville Skyttä authored and Paulus Schoutsen committed 2020-01-03 14:47:06 +01:00
1 parent 5ad209c6fd
commit fa4fa30461
105 files changed
+241 -314

No files matched your search

+1 -1
View File
@@ -155,7 +155,7 @@ class TestHDDTempSensor(unittest.TestCase):
assert len(self.hass.states.all()) == 1
state = self.hass.states.get("sensor.hd_temperature_dev_sdx1")
assert state.attributes.get("friendly_name") == "HD Temperature " + "/dev/sdx1"
assert state.attributes.get("friendly_name") == "HD Temperature /dev/sdx1"
@patch("telnetlib.Telnet", new=TelnetMock)
def test_hddtemp_multiple_disks(self):
+1 -1
View File
@@ -372,7 +372,7 @@ class TestLight(unittest.TestCase):
return StringIO(profile_data)
return real_open(path, *args, **kwargs)
profile_data = "id,x,y,brightness\n" + "group.all_lights.default,.4,.6,99\n"
profile_data = "id,x,y,brightness\ngroup.all_lights.default,.4,.6,99\n"
with mock.patch("os.path.isfile", side_effect=_mock_isfile):
with mock.patch("builtins.open", side_effect=_mock_open):
with mock_storage():
+5 -5
View File
@@ -156,12 +156,12 @@ def mock_light(test_features={}, test_state={}, n=0):
mock_light_data = Mock(**test_state)
mock_light = Mock(
id="mock-light-id-{}".format(n),
id=f"mock-light-id-{n}",
reachable=True,
observe=Mock(),
device_info=MagicMock(),
)
mock_light.name = "tradfri_light_{}".format(n)
mock_light.name = f"tradfri_light_{n}"
# Set supported features for the light.
features = {**DEFAULT_TEST_FEATURES, **test_features}
@@ -258,7 +258,7 @@ async def test_turn_on(
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": "light.tradfri_light_{}".format(id), **test_data},
{"entity_id": f"light.tradfri_light_{id}", **test_data},
blocking=True,
)
await hass.async_block_till_done()
@@ -286,7 +286,7 @@ async def test_turn_on(
await hass.async_block_till_done()
# Check that the state is correct.
states = hass.states.get("light.tradfri_light_{}".format(id))
states = hass.states.get(f"light.tradfri_light_{id}")
for k, v in expected_result.items():
if k == "state":
assert states.state == v
@@ -342,7 +342,7 @@ def mock_group(test_state={}, n=0):
state = {**default_state, **test_state}
mock_group = Mock(member_ids=[], observe=Mock(), **state)
mock_group.name = "tradfri_group_{}".format(n)
mock_group.name = f"tradfri_group_{n}"
return mock_group
+2 -2
View File
@@ -63,7 +63,7 @@ class TestUkTransportSensor(unittest.TestCase):
bus_state = self.hass.states.get("sensor.next_bus_to_wantage")
assert type(bus_state.state) == str
assert bus_state.name == "Next bus to {}".format(BUS_DIRECTION)
assert bus_state.name == f"Next bus to {BUS_DIRECTION}"
assert bus_state.attributes.get(ATTR_ATCOCODE) == BUS_ATCOCODE
assert bus_state.attributes.get(ATTR_LOCALITY) == "Harwell Campus"
assert bus_state.attributes.get(ATTR_STOP_NAME) == "Bus Station"
@@ -85,7 +85,7 @@ class TestUkTransportSensor(unittest.TestCase):
train_state = self.hass.states.get("sensor.next_train_to_WAT")
assert type(train_state.state) == str
assert train_state.name == "Next train to {}".format(TRAIN_DESTINATION_NAME)
assert train_state.name == f"Next train to {TRAIN_DESTINATION_NAME}"
assert train_state.attributes.get(ATTR_STATION_CODE) == TRAIN_STATION_CODE
assert train_state.attributes.get(ATTR_CALLING_AT) == TRAIN_DESTINATION_NAME
assert len(train_state.attributes.get(ATTR_NEXT_TRAINS)) == 25
@@ -25,9 +25,7 @@ from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, load_fixture, mock_component
scanner_path = (
"homeassistant.components.unifi_direct.device_tracker." + "UnifiDeviceScanner"
)
scanner_path = "homeassistant.components.unifi_direct.device_tracker.UnifiDeviceScanner"
@pytest.fixture(autouse=True)
@@ -118,14 +118,14 @@ async def test_if_fires_on_state_change(hass, calls):
hass.states.async_set("vacuum.entity", STATE_CLEANING)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "cleaning - device - {} - docked - cleaning".format(
"vacuum.entity"
assert (
calls[0].data["some"] == "cleaning - device - vacuum.entity - docked - cleaning"
)
# Fake that the entity is docked
hass.states.async_set("vacuum.entity", STATE_DOCKED)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "docked - device - {} - cleaning - docked".format(
"vacuum.entity"
assert (
calls[1].data["some"] == "docked - device - vacuum.entity - cleaning - docked"
)
+6 -8
View File
@@ -24,13 +24,13 @@ async def test_unregistering_webhook(hass, mock_client):
hass.components.webhook.async_register("test", "Test hook", webhook_id, handle)
resp = await mock_client.post("/api/webhook/{}".format(webhook_id))
resp = await mock_client.post(f"/api/webhook/{webhook_id}")
assert resp.status == 200
assert len(hooks) == 1
hass.components.webhook.async_unregister(webhook_id)
resp = await mock_client.post("/api/webhook/{}".format(webhook_id))
resp = await mock_client.post(f"/api/webhook/{webhook_id}")
assert resp.status == 200
assert len(hooks) == 1
@@ -73,9 +73,7 @@ async def test_posting_webhook_json(hass, mock_client):
hass.components.webhook.async_register("test", "Test hook", webhook_id, handle)
resp = await mock_client.post(
"/api/webhook/{}".format(webhook_id), json={"data": True}
)
resp = await mock_client.post(f"/api/webhook/{webhook_id}", json={"data": True})
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
@@ -94,7 +92,7 @@ async def test_posting_webhook_no_data(hass, mock_client):
hass.components.webhook.async_register("test", "Test hook", webhook_id, handle)
resp = await mock_client.post("/api/webhook/{}".format(webhook_id))
resp = await mock_client.post(f"/api/webhook/{webhook_id}")
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
@@ -114,7 +112,7 @@ async def test_webhook_put(hass, mock_client):
hass.components.webhook.async_register("test", "Test hook", webhook_id, handle)
resp = await mock_client.put("/api/webhook/{}".format(webhook_id))
resp = await mock_client.put(f"/api/webhook/{webhook_id}")
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
@@ -133,7 +131,7 @@ async def test_webhook_head(hass, mock_client):
hass.components.webhook.async_register("test", "Test hook", webhook_id, handle)
resp = await mock_client.head("/api/webhook/{}".format(webhook_id))
resp = await mock_client.head(f"/api/webhook/{webhook_id}")
assert resp.status == 200
assert len(hooks) == 1
assert hooks[0][0] is hass
+4 -5
View File
@@ -38,12 +38,11 @@ def assert_state_equals(
entity_id = get_entity_id(measure, profile)
state_obj = hass.states.get(entity_id)
assert state_obj, "Expected entity {} to exist but it did not".format(entity_id)
assert state_obj, f"Expected entity {entity_id} to exist but it did not"
assert state_obj.state == str(
expected
), "Expected {} but was {} for measure {}, {}".format(
expected, state_obj.state, measure, entity_id
assert state_obj.state == str(expected), (
f"Expected {expected} but was {state_obj.state} "
f"for measure {measure}, {entity_id}"
)
+2 -2
View File
@@ -174,7 +174,7 @@ async def test_xiaomi_exceptions(hass, caplog, mock_mirobo_errors):
async def test_xiaomi_vacuum_services(hass, caplog, mock_mirobo_is_got_error):
"""Test vacuum supported features."""
entity_name = "test_vacuum_cleaner_1"
entity_id = "{}.{}".format(DOMAIN, entity_name)
entity_id = f"{DOMAIN}.{entity_name}"
await async_setup_component(
hass,
@@ -319,7 +319,7 @@ async def test_xiaomi_vacuum_services(hass, caplog, mock_mirobo_is_got_error):
async def test_xiaomi_specific_services(hass, caplog, mock_mirobo_is_on):
"""Test vacuum supported features."""
entity_name = "test_vacuum_cleaner_2"
entity_id = "{}.{}".format(DOMAIN, entity_name)
entity_id = f"{DOMAIN}.{entity_name}"
await async_setup_component(
hass,
+1 -3
View File
@@ -129,9 +129,7 @@ async def test_connection_error_on_init(hass, caplog, valid_settings, connection
and record.name == "homeassistant.components.yessssms.notify"
):
assert (
"Connection Error, could not verify login data for '{}'".format(
"educom"
)
"Connection Error, could not verify login data for 'educom'"
in record.message
)
for record in caplog.records:
+1 -1
View File
@@ -113,7 +113,7 @@ class YahooWeatherMock:
def getWeatherImage(self, code): # pylint: disable=invalid-name
"""Create a link to weather image from yahoo code."""
return "https://l.yimg.com/a/i/us/we/52/{}.gif".format(code)
return f"https://l.yimg.com/a/i/us/we/52/{code}.gif"
class TestWeather(unittest.TestCase):
+1 -3
View File
@@ -18,9 +18,7 @@ def mock_zeroconf():
def service_update_mock(zeroconf, service, handlers):
"""Call service update handler."""
handlers[0](
zeroconf, service, "{}.{}".format("name", service), ServiceStateChange.Added
)
handlers[0](zeroconf, service, f"name.{service}", ServiceStateChange.Added)
def get_service_info_mock(service_type, name):
+3 -3
View File
@@ -110,9 +110,9 @@ async def async_build_devices(hass, zha_gateway, config_entry, cluster_ids):
[],
None,
zha_gateway,
ieee="00:15:8d:00:02:32:4f:0{}".format(counter),
manufacturer="Fake{}".format(cluster_id),
model="FakeModel{}".format(cluster_id),
ieee=f"00:15:8d:00:02:32:4f:0{counter}",
manufacturer=f"Fake{cluster_id}",
model=f"FakeModel{cluster_id}",
)
counter += 1
+9 -9
View File
@@ -459,18 +459,18 @@ async def test_value_entities(hass, mock_openzwave):
entry = ent_reg.async_get("zwave.mock_node")
assert entry is not None
assert entry.unique_id == "node-{}".format(node.node_id)
assert entry.unique_id == f"node-{node.node_id}"
node_dev_id = entry.device_id
entry = ent_reg.async_get("binary_sensor.mock_node_mock_value")
assert entry is not None
assert entry.unique_id == "{}-{}".format(node.node_id, value.object_id)
assert entry.unique_id == f"{node.node_id}-{value.object_id}"
assert entry.name is None
assert entry.device_id == node_dev_id
entry = ent_reg.async_get("binary_sensor.mock_node_mock_value_b")
assert entry is not None
assert entry.unique_id == "{}-{}".format(node.node_id, value2.object_id)
assert entry.unique_id == f"{node.node_id}-{value2.object_id}"
assert entry.name is None
assert entry.device_id != node_dev_id
device_id_b = entry.device_id
@@ -482,7 +482,7 @@ async def test_value_entities(hass, mock_openzwave):
device = dev_reg.async_get(device_id_b)
assert device is not None
assert device.name == "{} ({})".format(node.name, value2.instance)
assert device.name == f"{node.name} ({value2.instance})"
# test renaming without updating
await hass.services.async_call(
@@ -510,7 +510,7 @@ async def test_value_entities(hass, mock_openzwave):
device = dev_reg.async_get(device_id_b)
assert device is not None
assert device.name == "{} ({})".format(node.name, value2.instance)
assert device.name == f"{node.name} ({value2.instance})"
# test renaming
await hass.services.async_call(
@@ -528,11 +528,11 @@ async def test_value_entities(hass, mock_openzwave):
entry = ent_reg.async_get("zwave.new_node")
assert entry is not None
assert entry.unique_id == "node-{}".format(node.node_id)
assert entry.unique_id == f"node-{node.node_id}"
entry = ent_reg.async_get("binary_sensor.new_node_mock_value")
assert entry is not None
assert entry.unique_id == "{}-{}".format(node.node_id, value.object_id)
assert entry.unique_id == f"{node.node_id}-{value.object_id}"
device = dev_reg.async_get(node_dev_id)
assert device is not None
@@ -541,7 +541,7 @@ async def test_value_entities(hass, mock_openzwave):
device = dev_reg.async_get(device_id_b)
assert device is not None
assert device.name == "{} ({})".format(node.name, value2.instance)
assert device.name == f"{node.name} ({value2.instance})"
await hass.services.async_call(
"zwave",
@@ -557,7 +557,7 @@ async def test_value_entities(hass, mock_openzwave):
entry = ent_reg.async_get("binary_sensor.new_node_new_label")
assert entry is not None
assert entry.unique_id == "{}-{}".format(node.node_id, value.object_id)
assert entry.unique_id == f"{node.node_id}-{value.object_id}"
async def test_value_discovery_existing_entity(hass, mock_openzwave):