mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
SNMP sensor refactor to ManualTriggerSensorEntity (#98630)
* SNMP to ManualTriggerSensorEntity * Mods
This commit is contained in:
parent
3b31c58eba
commit
31a8a62165
@ -19,11 +19,15 @@ from pysnmp.hlapi.asyncio import (
|
|||||||
)
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import CONF_STATE_CLASS, PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
CONF_DEVICE_CLASS,
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
|
CONF_ICON,
|
||||||
|
CONF_NAME,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
@ -31,9 +35,12 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.template import Template
|
||||||
from homeassistant.helpers.template_entity import (
|
from homeassistant.helpers.template_entity import (
|
||||||
|
CONF_AVAILABILITY,
|
||||||
|
CONF_PICTURE,
|
||||||
TEMPLATE_SENSOR_BASE_SCHEMA,
|
TEMPLATE_SENSOR_BASE_SCHEMA,
|
||||||
TemplateSensor,
|
ManualTriggerSensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
@ -64,6 +71,16 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=10)
|
SCAN_INTERVAL = timedelta(seconds=10)
|
||||||
|
|
||||||
|
TRIGGER_ENTITY_OPTIONS = (
|
||||||
|
CONF_AVAILABILITY,
|
||||||
|
CONF_DEVICE_CLASS,
|
||||||
|
CONF_ICON,
|
||||||
|
CONF_PICTURE,
|
||||||
|
CONF_UNIQUE_ID,
|
||||||
|
CONF_STATE_CLASS,
|
||||||
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
|
)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_BASEOID): cv.string,
|
vol.Required(CONF_BASEOID): cv.string,
|
||||||
@ -106,7 +123,6 @@ async def async_setup_platform(
|
|||||||
privproto = config[CONF_PRIV_PROTOCOL]
|
privproto = config[CONF_PRIV_PROTOCOL]
|
||||||
accept_errors = config.get(CONF_ACCEPT_ERRORS)
|
accept_errors = config.get(CONF_ACCEPT_ERRORS)
|
||||||
default_value = config.get(CONF_DEFAULT_VALUE)
|
default_value = config.get(CONF_DEFAULT_VALUE)
|
||||||
unique_id = config.get(CONF_UNIQUE_ID)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Try IPv4 first.
|
# Try IPv4 first.
|
||||||
@ -151,35 +167,50 @@ async def async_setup_platform(
|
|||||||
_LOGGER.error("Please check the details in the configuration file")
|
_LOGGER.error("Please check the details in the configuration file")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
name = config.get(CONF_NAME, Template(DEFAULT_NAME, hass))
|
||||||
|
trigger_entity_config = {CONF_NAME: name}
|
||||||
|
for key in TRIGGER_ENTITY_OPTIONS:
|
||||||
|
if key not in config:
|
||||||
|
continue
|
||||||
|
trigger_entity_config[key] = config[key]
|
||||||
|
|
||||||
|
value_template: Template | None = config.get(CONF_VALUE_TEMPLATE)
|
||||||
|
if value_template is not None:
|
||||||
|
value_template.hass = hass
|
||||||
|
|
||||||
data = SnmpData(request_args, baseoid, accept_errors, default_value)
|
data = SnmpData(request_args, baseoid, accept_errors, default_value)
|
||||||
async_add_entities([SnmpSensor(hass, data, config, unique_id)], True)
|
async_add_entities([SnmpSensor(hass, data, trigger_entity_config, value_template)])
|
||||||
|
|
||||||
|
|
||||||
class SnmpSensor(TemplateSensor):
|
class SnmpSensor(ManualTriggerSensorEntity):
|
||||||
"""Representation of a SNMP sensor."""
|
"""Representation of a SNMP sensor."""
|
||||||
|
|
||||||
_attr_should_poll = True
|
_attr_should_poll = True
|
||||||
|
|
||||||
def __init__(self, hass, data, config, unique_id):
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
data: SnmpData,
|
||||||
|
config: ConfigType,
|
||||||
|
value_template: Template | None,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(
|
super().__init__(hass, config)
|
||||||
hass, config=config, unique_id=unique_id, fallback_name=DEFAULT_NAME
|
|
||||||
)
|
|
||||||
self.data = data
|
self.data = data
|
||||||
self._state = None
|
self._state = None
|
||||||
self._value_template = config.get(CONF_VALUE_TEMPLATE)
|
self._value_template = value_template
|
||||||
if (value_template := self._value_template) is not None:
|
|
||||||
value_template.hass = hass
|
|
||||||
|
|
||||||
@property
|
async def async_added_to_hass(self) -> None:
|
||||||
def native_value(self):
|
"""Handle adding to Home Assistant."""
|
||||||
"""Return the state of the sensor."""
|
await super().async_added_to_hass()
|
||||||
return self._state
|
await self.async_update()
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
await self.data.async_update()
|
await self.data.async_update()
|
||||||
|
|
||||||
|
raw_value = self.data.value
|
||||||
|
|
||||||
if (value := self.data.value) is None:
|
if (value := self.data.value) is None:
|
||||||
value = STATE_UNKNOWN
|
value = STATE_UNKNOWN
|
||||||
elif self._value_template is not None:
|
elif self._value_template is not None:
|
||||||
@ -187,13 +218,14 @@ class SnmpSensor(TemplateSensor):
|
|||||||
value, STATE_UNKNOWN
|
value, STATE_UNKNOWN
|
||||||
)
|
)
|
||||||
|
|
||||||
self._state = value
|
self._attr_native_value = value
|
||||||
|
self._process_manual_data(raw_value)
|
||||||
|
|
||||||
|
|
||||||
class SnmpData:
|
class SnmpData:
|
||||||
"""Get the latest data and update the states."""
|
"""Get the latest data and update the states."""
|
||||||
|
|
||||||
def __init__(self, request_args, baseoid, accept_errors, default_value):
|
def __init__(self, request_args, baseoid, accept_errors, default_value) -> None:
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self._request_args = request_args
|
self._request_args = request_args
|
||||||
self._baseoid = baseoid
|
self._baseoid = baseoid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user