Skip to content

Commit

Permalink
Cleanup ManualTriggerSensorEntity (#98629)
Browse files Browse the repository at this point in the history
* Cleanup ManualTriggerSensorEntity

* ConfigType
  • Loading branch information
gjohansson-ST committed Aug 18, 2023
1 parent 7fcc2dd commit 268e524
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 102 deletions.
40 changes: 22 additions & 18 deletions homeassistant/components/command_line/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
PLATFORM_SCHEMA,
STATE_CLASSES_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.components.sensor.helpers import async_parse_date_datetime
from homeassistant.const import (
CONF_COMMAND,
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_NAME,
CONF_SCAN_INTERVAL,
CONF_UNIQUE_ID,
Expand All @@ -36,7 +35,11 @@
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.template import Template
from homeassistant.helpers.template_entity import ManualTriggerEntity
from homeassistant.helpers.template_entity import (
CONF_AVAILABILITY,
CONF_PICTURE,
ManualTriggerSensorEntity,
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util

Expand All @@ -47,6 +50,16 @@

DEFAULT_NAME = "Command Sensor"

TRIGGER_ENTITY_OPTIONS = (
CONF_AVAILABILITY,
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_PICTURE,
CONF_UNIQUE_ID,
CONF_STATE_CLASS,
CONF_UNIT_OF_MEASUREMENT,
)

SCAN_INTERVAL = timedelta(seconds=60)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand Down Expand Up @@ -87,30 +100,25 @@ async def async_setup_platform(

name: str = sensor_config[CONF_NAME]
command: str = sensor_config[CONF_COMMAND]
unit: str | None = sensor_config.get(CONF_UNIT_OF_MEASUREMENT)
value_template: Template | None = sensor_config.get(CONF_VALUE_TEMPLATE)
command_timeout: int = sensor_config[CONF_COMMAND_TIMEOUT]
unique_id: str | None = sensor_config.get(CONF_UNIQUE_ID)
if value_template is not None:
value_template.hass = hass
json_attributes: list[str] | None = sensor_config.get(CONF_JSON_ATTRIBUTES)
scan_interval: timedelta = sensor_config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)
state_class: SensorStateClass | None = sensor_config.get(CONF_STATE_CLASS)
data = CommandSensorData(hass, command, command_timeout)

trigger_entity_config = {
CONF_UNIQUE_ID: unique_id,
CONF_NAME: Template(name, hass),
CONF_DEVICE_CLASS: sensor_config.get(CONF_DEVICE_CLASS),
}
trigger_entity_config = {CONF_NAME: Template(name, hass)}
for key in TRIGGER_ENTITY_OPTIONS:
if key not in sensor_config:
continue
trigger_entity_config[key] = sensor_config[key]

async_add_entities(
[
CommandSensor(
data,
trigger_entity_config,
unit,
state_class,
value_template,
json_attributes,
scan_interval,
Expand All @@ -119,7 +127,7 @@ async def async_setup_platform(
)


class CommandSensor(ManualTriggerEntity, SensorEntity):
class CommandSensor(ManualTriggerSensorEntity):
"""Representation of a sensor that is using shell commands."""

_attr_should_poll = False
Expand All @@ -128,8 +136,6 @@ def __init__(
self,
data: CommandSensorData,
config: ConfigType,
unit_of_measurement: str | None,
state_class: SensorStateClass | None,
value_template: Template | None,
json_attributes: list[str] | None,
scan_interval: timedelta,
Expand All @@ -141,8 +147,6 @@ def __init__(
self._json_attributes = json_attributes
self._attr_native_value = None
self._value_template = value_template
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_state_class = state_class
self._scan_interval = scan_interval
self._process_updates: asyncio.Lock | None = None

Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/rest/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
DOMAIN as SENSOR_DOMAIN,
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.components.sensor.helpers import async_parse_date_datetime
from homeassistant.const import (
Expand Down Expand Up @@ -118,7 +117,7 @@ async def async_setup_platform(
)


class RestSensor(ManualTriggerSensorEntity, RestEntity, SensorEntity):
class RestSensor(ManualTriggerSensorEntity, RestEntity):
"""Implementation of a REST sensor."""

def __init__(
Expand Down
57 changes: 24 additions & 33 deletions homeassistant/components/scrape/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@

import voluptuous as vol

from homeassistant.components.sensor import (
CONF_STATE_CLASS,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorDeviceClass
from homeassistant.components.sensor.helpers import async_parse_date_datetime
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand All @@ -32,6 +28,7 @@
CONF_PICTURE,
TEMPLATE_SENSOR_BASE_SCHEMA,
ManualTriggerEntity,
ManualTriggerSensorEntity,
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand All @@ -41,6 +38,16 @@

_LOGGER = logging.getLogger(__name__)

TRIGGER_ENTITY_OPTIONS = (
CONF_AVAILABILITY,
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_PICTURE,
CONF_UNIQUE_ID,
CONF_STATE_CLASS,
CONF_UNIT_OF_MEASUREMENT,
)


async def async_setup_platform(
hass: HomeAssistant,
Expand All @@ -63,25 +70,17 @@ async def async_setup_platform(
if value_template is not None:
value_template.hass = hass

trigger_entity_config = {
CONF_NAME: sensor_config[CONF_NAME],
CONF_DEVICE_CLASS: sensor_config.get(CONF_DEVICE_CLASS),
CONF_UNIQUE_ID: sensor_config.get(CONF_UNIQUE_ID),
}
if available := sensor_config.get(CONF_AVAILABILITY):
trigger_entity_config[CONF_AVAILABILITY] = available
if icon := sensor_config.get(CONF_ICON):
trigger_entity_config[CONF_ICON] = icon
if picture := sensor_config.get(CONF_PICTURE):
trigger_entity_config[CONF_PICTURE] = picture
trigger_entity_config = {CONF_NAME: sensor_config[CONF_NAME]}
for key in TRIGGER_ENTITY_OPTIONS:
if key not in sensor_config:
continue
trigger_entity_config[key] = sensor_config[key]

entities.append(
ScrapeSensor(
hass,
coordinator,
trigger_entity_config,
sensor_config.get(CONF_UNIT_OF_MEASUREMENT),
sensor_config.get(CONF_STATE_CLASS),
sensor_config[CONF_SELECT],
sensor_config.get(CONF_ATTRIBUTE),
sensor_config[CONF_INDEX],
Expand Down Expand Up @@ -113,19 +112,17 @@ async def async_setup_entry(
Template(value_string, hass) if value_string is not None else None
)

trigger_entity_config = {
CONF_NAME: name,
CONF_DEVICE_CLASS: sensor_config.get(CONF_DEVICE_CLASS),
CONF_UNIQUE_ID: sensor_config[CONF_UNIQUE_ID],
}
trigger_entity_config = {CONF_NAME: name}
for key in TRIGGER_ENTITY_OPTIONS:
if key not in sensor_config:
continue
trigger_entity_config[key] = sensor_config[key]

entities.append(
ScrapeSensor(
hass,
coordinator,
trigger_entity_config,
sensor_config.get(CONF_UNIT_OF_MEASUREMENT),
sensor_config.get(CONF_STATE_CLASS),
sensor_config[CONF_SELECT],
sensor_config.get(CONF_ATTRIBUTE),
sensor_config[CONF_INDEX],
Expand All @@ -137,18 +134,14 @@ async def async_setup_entry(
async_add_entities(entities)


class ScrapeSensor(
CoordinatorEntity[ScrapeCoordinator], ManualTriggerEntity, SensorEntity
):
class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEntity):
"""Representation of a web scrape sensor."""

def __init__(
self,
hass: HomeAssistant,
coordinator: ScrapeCoordinator,
trigger_entity_config: ConfigType,
unit_of_measurement: str | None,
state_class: str | None,
select: str,
attr: str | None,
index: int,
Expand All @@ -157,9 +150,7 @@ def __init__(
) -> None:
"""Initialize a web scrape sensor."""
CoordinatorEntity.__init__(self, coordinator)
ManualTriggerEntity.__init__(self, hass, trigger_entity_config)
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_state_class = state_class
ManualTriggerSensorEntity.__init__(self, hass, trigger_entity_config)
self._select = select
self._attr = attr
self._index = index
Expand Down
Loading

0 comments on commit 268e524

Please sign in to comment.