mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Fix Command Line template error when data is None (#97845)
Command Line template error
This commit is contained in:
parent
c5e5567912
commit
2e263560ec
@ -207,7 +207,8 @@ class CommandSensor(ManualTriggerEntity, SensorEntity):
|
|||||||
self._process_manual_data(value)
|
self._process_manual_data(value)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._value_template is not None:
|
self._attr_native_value = None
|
||||||
|
if self._value_template is not None and value is not None:
|
||||||
value = self._value_template.async_render_with_possible_json_value(
|
value = self._value_template.async_render_with_possible_json_value(
|
||||||
value,
|
value,
|
||||||
None,
|
None,
|
||||||
@ -221,7 +222,6 @@ class CommandSensor(ManualTriggerEntity, SensorEntity):
|
|||||||
self._process_manual_data(value)
|
self._process_manual_data(value)
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attr_native_value = None
|
|
||||||
if value is not None:
|
if value is not None:
|
||||||
self._attr_native_value = async_parse_date_datetime(
|
self._attr_native_value = async_parse_date_datetime(
|
||||||
value, self.entity_id, self.device_class
|
value, self.entity_id, self.device_class
|
||||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import subprocess
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ from homeassistant.components.homeassistant import (
|
|||||||
SERVICE_UPDATE_ENTITY,
|
SERVICE_UPDATE_ENTITY,
|
||||||
)
|
)
|
||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
import homeassistant.helpers.issue_registry as ir
|
import homeassistant.helpers.issue_registry as ir
|
||||||
@ -698,3 +699,40 @@ async def test_scrape_sensor_device_date(
|
|||||||
entity_state = hass.states.get("sensor.test")
|
entity_state = hass.states.get("sensor.test")
|
||||||
assert entity_state
|
assert entity_state
|
||||||
assert entity_state.state == "2022-01-17"
|
assert entity_state.state == "2022-01-17"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_template_not_error_when_data_is_none(
|
||||||
|
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||||
|
) -> None:
|
||||||
|
"""Test command sensor with template not logging error when data is None."""
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.command_line.utils.subprocess.check_output",
|
||||||
|
side_effect=subprocess.CalledProcessError,
|
||||||
|
):
|
||||||
|
await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
{
|
||||||
|
"command_line": [
|
||||||
|
{
|
||||||
|
"sensor": {
|
||||||
|
"name": "Test",
|
||||||
|
"command": "failed command",
|
||||||
|
"unit_of_measurement": "MB",
|
||||||
|
"value_template": "{{ (value.split('\t')[0]|int(0)/1000)|round(3) }}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
entity_state = hass.states.get("sensor.test")
|
||||||
|
assert entity_state
|
||||||
|
assert entity_state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"Template variable error: 'None' has no attribute 'split' when rendering"
|
||||||
|
not in caplog.text
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user