Improve code quality command_line (#65333)

This commit is contained in:
G Johansson 2022-02-12 15:19:37 +01:00 committed by GitHub
parent db6969739f
commit 3771c154fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 190 additions and 205 deletions

View file

@ -19,10 +19,10 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.reload import setup_reload_service
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import check_output_or_log
@ -59,23 +59,19 @@ def setup_platform(
setup_reload_service(hass, DOMAIN, PLATFORMS)
name = config.get(CONF_NAME)
command = config.get(CONF_COMMAND)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE)
command_timeout = config.get(CONF_COMMAND_TIMEOUT)
unique_id = config.get(CONF_UNIQUE_ID)
name: str = config[CONF_NAME]
command: str = config[CONF_COMMAND]
unit: str | None = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template: Template | None = config.get(CONF_VALUE_TEMPLATE)
command_timeout: int = config[CONF_COMMAND_TIMEOUT]
unique_id: str | None = config.get(CONF_UNIQUE_ID)
if value_template is not None:
value_template.hass = hass
json_attributes = config.get(CONF_JSON_ATTRIBUTES)
json_attributes: list[str] | None = config.get(CONF_JSON_ATTRIBUTES)
data = CommandSensorData(hass, command, command_timeout)
add_entities(
[
CommandSensor(
hass, data, name, unit, value_template, json_attributes, unique_id
)
],
[CommandSensor(data, name, unit, value_template, json_attributes, unique_id)],
True,
)
@ -85,57 +81,35 @@ class CommandSensor(SensorEntity):
def __init__(
self,
hass,
data,
name,
unit_of_measurement,
value_template,
json_attributes,
unique_id,
):
data: CommandSensorData,
name: str,
unit_of_measurement: str | None,
value_template: Template | None,
json_attributes: list[str] | None,
unique_id: str | None,
) -> None:
"""Initialize the sensor."""
self._hass = hass
self.data = data
self._attributes = None
self._attr_extra_state_attributes = {}
self._json_attributes = json_attributes
self._name = name
self._state = None
self._unit_of_measurement = unit_of_measurement
self._attr_name = name
self._attr_native_value = None
self._attr_native_unit_of_measurement = unit_of_measurement
self._value_template = value_template
self._attr_unique_id = unique_id
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
@property
def native_value(self):
"""Return the state of the device."""
return self._state
@property
def extra_state_attributes(self):
"""Return the state attributes."""
return self._attributes
def update(self):
def update(self) -> None:
"""Get the latest data and updates the state."""
self.data.update()
value = self.data.value
if self._json_attributes:
self._attributes = {}
self._attr_extra_state_attributes = {}
if value:
try:
json_dict = json.loads(value)
if isinstance(json_dict, Mapping):
self._attributes = {
self._attr_extra_state_attributes = {
k: json_dict[k]
for k in self._json_attributes
if k in json_dict
@ -150,24 +124,26 @@ class CommandSensor(SensorEntity):
if value is None:
value = STATE_UNKNOWN
elif self._value_template is not None:
self._state = self._value_template.render_with_possible_json_value(
value, STATE_UNKNOWN
self._attr_native_value = (
self._value_template.render_with_possible_json_value(
value, STATE_UNKNOWN
)
)
else:
self._state = value
self._attr_native_value = value
class CommandSensorData:
"""The class for handling the data retrieval."""
def __init__(self, hass, command, command_timeout):
def __init__(self, hass: HomeAssistant, command: str, command_timeout: int) -> None:
"""Initialize the data object."""
self.value = None
self.value: str | None = None
self.hass = hass
self.command = command
self.timeout = command_timeout
def update(self):
def update(self) -> None:
"""Get the latest data with a shell command."""
command = self.command
@ -177,7 +153,7 @@ class CommandSensorData:
args_compiled = None
else:
prog, args = command.split(" ", 1)
args_compiled = template.Template(args, self.hass)
args_compiled = Template(args, self.hass)
if args_compiled:
try: