mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Rename MQTT mixin classes (#118039)
This commit is contained in:
parent
080bba5d9b
commit
6f81852eb4
@ -38,7 +38,7 @@ from homeassistant.util import dt as dt_util
|
|||||||
from . import subscription
|
from . import subscription
|
||||||
from .config import MQTT_RO_SCHEMA
|
from .config import MQTT_RO_SCHEMA
|
||||||
from .const import CONF_ENCODING, CONF_QOS, CONF_STATE_TOPIC, PAYLOAD_NONE
|
from .const import CONF_ENCODING, CONF_QOS, CONF_STATE_TOPIC, PAYLOAD_NONE
|
||||||
from .mixins import MqttAvailability, MqttEntity, async_setup_entity_entry_helper
|
from .mixins import MqttAvailabilityMixin, MqttEntity, async_setup_entity_entry_helper
|
||||||
from .models import MqttValueTemplate, ReceiveMessage
|
from .models import MqttValueTemplate, ReceiveMessage
|
||||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||||
|
|
||||||
@ -268,6 +268,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return true if the device is available and value has not expired."""
|
"""Return true if the device is available and value has not expired."""
|
||||||
# mypy doesn't know about fget: https://github.com/python/mypy/issues/6185
|
# mypy doesn't know about fget: https://github.com/python/mypy/issues/6185
|
||||||
return MqttAvailability.available.fget(self) and ( # type: ignore[attr-defined]
|
return MqttAvailabilityMixin.available.fget(self) and ( # type: ignore[attr-defined]
|
||||||
self._expire_after is None or not self._expired
|
self._expire_after is None or not self._expired
|
||||||
)
|
)
|
||||||
|
@ -36,7 +36,7 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from .discovery import MQTTDiscoveryPayload, clear_discovery_hash
|
from .discovery import MQTTDiscoveryPayload, clear_discovery_hash
|
||||||
from .mixins import MqttDiscoveryDeviceUpdate, send_discovery_done, update_device
|
from .mixins import MqttDiscoveryDeviceUpdateMixin, send_discovery_done, update_device
|
||||||
from .models import DATA_MQTT
|
from .models import DATA_MQTT
|
||||||
from .schemas import MQTT_ENTITY_DEVICE_INFO_SCHEMA
|
from .schemas import MQTT_ENTITY_DEVICE_INFO_SCHEMA
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ class Trigger:
|
|||||||
trig.remove = None
|
trig.remove = None
|
||||||
|
|
||||||
|
|
||||||
class MqttDeviceTrigger(MqttDiscoveryDeviceUpdate):
|
class MqttDeviceTrigger(MqttDiscoveryDeviceUpdateMixin):
|
||||||
"""Setup a MQTT device trigger with auto discovery."""
|
"""Setup a MQTT device trigger with auto discovery."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -205,7 +205,7 @@ class MqttDeviceTrigger(MqttDiscoveryDeviceUpdate):
|
|||||||
self._mqtt_data = hass.data[DATA_MQTT]
|
self._mqtt_data = hass.data[DATA_MQTT]
|
||||||
self.trigger_id = f"{device_id}_{config[CONF_TYPE]}_{config[CONF_SUBTYPE]}"
|
self.trigger_id = f"{device_id}_{config[CONF_TYPE]}_{config[CONF_SUBTYPE]}"
|
||||||
|
|
||||||
MqttDiscoveryDeviceUpdate.__init__(
|
MqttDiscoveryDeviceUpdateMixin.__init__(
|
||||||
self,
|
self,
|
||||||
hass,
|
hass,
|
||||||
discovery_data,
|
discovery_data,
|
||||||
|
@ -398,7 +398,7 @@ def write_state_on_attr_change(
|
|||||||
return _decorator
|
return _decorator
|
||||||
|
|
||||||
|
|
||||||
class MqttAttributes(Entity):
|
class MqttAttributesMixin(Entity):
|
||||||
"""Mixin used for platforms that support JSON attributes."""
|
"""Mixin used for platforms that support JSON attributes."""
|
||||||
|
|
||||||
_attributes_extra_blocked: frozenset[str] = frozenset()
|
_attributes_extra_blocked: frozenset[str] = frozenset()
|
||||||
@ -480,7 +480,7 @@ class MqttAttributes(Entity):
|
|||||||
_LOGGER.warning("JSON result was not a dictionary")
|
_LOGGER.warning("JSON result was not a dictionary")
|
||||||
|
|
||||||
|
|
||||||
class MqttAvailability(Entity):
|
class MqttAvailabilityMixin(Entity):
|
||||||
"""Mixin used for platforms that report availability."""
|
"""Mixin used for platforms that report availability."""
|
||||||
|
|
||||||
def __init__(self, config: ConfigType) -> None:
|
def __init__(self, config: ConfigType) -> None:
|
||||||
@ -687,7 +687,7 @@ async def async_clear_discovery_topic_if_entity_removed(
|
|||||||
await async_remove_discovery_payload(hass, discovery_data)
|
await async_remove_discovery_payload(hass, discovery_data)
|
||||||
|
|
||||||
|
|
||||||
class MqttDiscoveryDeviceUpdate(ABC):
|
class MqttDiscoveryDeviceUpdateMixin(ABC):
|
||||||
"""Add support for auto discovery for platforms without an entity."""
|
"""Add support for auto discovery for platforms without an entity."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -822,7 +822,7 @@ class MqttDiscoveryDeviceUpdate(ABC):
|
|||||||
"""Handle the cleanup of platform specific parts, extend to the platform."""
|
"""Handle the cleanup of platform specific parts, extend to the platform."""
|
||||||
|
|
||||||
|
|
||||||
class MqttDiscoveryUpdate(Entity):
|
class MqttDiscoveryUpdateMixin(Entity):
|
||||||
"""Mixin used to handle updated discovery message for entity based platforms."""
|
"""Mixin used to handle updated discovery message for entity based platforms."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -854,7 +854,7 @@ class MqttDiscoveryUpdate(Entity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def _async_remove_state_and_registry_entry(
|
async def _async_remove_state_and_registry_entry(
|
||||||
self: MqttDiscoveryUpdate,
|
self: MqttDiscoveryUpdateMixin,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Remove entity's state and entity registry entry.
|
"""Remove entity's state and entity registry entry.
|
||||||
|
|
||||||
@ -1076,9 +1076,9 @@ class MqttEntityDeviceInfo(Entity):
|
|||||||
|
|
||||||
|
|
||||||
class MqttEntity(
|
class MqttEntity(
|
||||||
MqttAttributes,
|
MqttAttributesMixin,
|
||||||
MqttAvailability,
|
MqttAvailabilityMixin,
|
||||||
MqttDiscoveryUpdate,
|
MqttDiscoveryUpdateMixin,
|
||||||
MqttEntityDeviceInfo,
|
MqttEntityDeviceInfo,
|
||||||
):
|
):
|
||||||
"""Representation of an MQTT entity."""
|
"""Representation of an MQTT entity."""
|
||||||
@ -1111,9 +1111,11 @@ class MqttEntity(
|
|||||||
self._init_entity_id()
|
self._init_entity_id()
|
||||||
|
|
||||||
# Initialize mixin classes
|
# Initialize mixin classes
|
||||||
MqttAttributes.__init__(self, config)
|
MqttAttributesMixin.__init__(self, config)
|
||||||
MqttAvailability.__init__(self, config)
|
MqttAvailabilityMixin.__init__(self, config)
|
||||||
MqttDiscoveryUpdate.__init__(self, hass, discovery_data, self.discovery_update)
|
MqttDiscoveryUpdateMixin.__init__(
|
||||||
|
self, hass, discovery_data, self.discovery_update
|
||||||
|
)
|
||||||
MqttEntityDeviceInfo.__init__(self, config.get(CONF_DEVICE), config_entry)
|
MqttEntityDeviceInfo.__init__(self, config.get(CONF_DEVICE), config_entry)
|
||||||
|
|
||||||
def _init_entity_id(self) -> None:
|
def _init_entity_id(self) -> None:
|
||||||
@ -1164,9 +1166,9 @@ class MqttEntity(
|
|||||||
self._sub_state = subscription.async_unsubscribe_topics(
|
self._sub_state = subscription.async_unsubscribe_topics(
|
||||||
self.hass, self._sub_state
|
self.hass, self._sub_state
|
||||||
)
|
)
|
||||||
await MqttAttributes.async_will_remove_from_hass(self)
|
await MqttAttributesMixin.async_will_remove_from_hass(self)
|
||||||
await MqttAvailability.async_will_remove_from_hass(self)
|
await MqttAvailabilityMixin.async_will_remove_from_hass(self)
|
||||||
await MqttDiscoveryUpdate.async_will_remove_from_hass(self)
|
await MqttDiscoveryUpdateMixin.async_will_remove_from_hass(self)
|
||||||
debug_info.remove_entity_data(self.hass, self.entity_id)
|
debug_info.remove_entity_data(self.hass, self.entity_id)
|
||||||
|
|
||||||
async def async_publish(
|
async def async_publish(
|
||||||
|
@ -41,7 +41,7 @@ from homeassistant.util import dt as dt_util
|
|||||||
from . import subscription
|
from . import subscription
|
||||||
from .config import MQTT_RO_SCHEMA
|
from .config import MQTT_RO_SCHEMA
|
||||||
from .const import CONF_ENCODING, CONF_QOS, CONF_STATE_TOPIC, PAYLOAD_NONE
|
from .const import CONF_ENCODING, CONF_QOS, CONF_STATE_TOPIC, PAYLOAD_NONE
|
||||||
from .mixins import MqttAvailability, MqttEntity, async_setup_entity_entry_helper
|
from .mixins import MqttAvailabilityMixin, MqttEntity, async_setup_entity_entry_helper
|
||||||
from .models import (
|
from .models import (
|
||||||
MqttValueTemplate,
|
MqttValueTemplate,
|
||||||
PayloadSentinel,
|
PayloadSentinel,
|
||||||
@ -318,6 +318,6 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return true if the device is available and value has not expired."""
|
"""Return true if the device is available and value has not expired."""
|
||||||
# mypy doesn't know about fget: https://github.com/python/mypy/issues/6185
|
# mypy doesn't know about fget: https://github.com/python/mypy/issues/6185
|
||||||
return MqttAvailability.available.fget(self) and ( # type: ignore[attr-defined]
|
return MqttAvailabilityMixin.available.fget(self) and ( # type: ignore[attr-defined]
|
||||||
self._expire_after is None or not self._expired
|
self._expire_after is None or not self._expired
|
||||||
)
|
)
|
||||||
|
@ -20,7 +20,7 @@ from .config import MQTT_BASE_SCHEMA
|
|||||||
from .const import ATTR_DISCOVERY_HASH, CONF_QOS, CONF_TOPIC
|
from .const import ATTR_DISCOVERY_HASH, CONF_QOS, CONF_TOPIC
|
||||||
from .discovery import MQTTDiscoveryPayload
|
from .discovery import MQTTDiscoveryPayload
|
||||||
from .mixins import (
|
from .mixins import (
|
||||||
MqttDiscoveryDeviceUpdate,
|
MqttDiscoveryDeviceUpdateMixin,
|
||||||
async_handle_schema_error,
|
async_handle_schema_error,
|
||||||
async_setup_non_entity_entry_helper,
|
async_setup_non_entity_entry_helper,
|
||||||
send_discovery_done,
|
send_discovery_done,
|
||||||
@ -97,7 +97,7 @@ def async_has_tags(hass: HomeAssistant, device_id: str) -> bool:
|
|||||||
return tags[device_id] != {}
|
return tags[device_id] != {}
|
||||||
|
|
||||||
|
|
||||||
class MQTTTagScanner(MqttDiscoveryDeviceUpdate):
|
class MQTTTagScanner(MqttDiscoveryDeviceUpdateMixin):
|
||||||
"""MQTT Tag scanner."""
|
"""MQTT Tag scanner."""
|
||||||
|
|
||||||
_value_template: Callable[[ReceivePayloadType, str], ReceivePayloadType]
|
_value_template: Callable[[ReceivePayloadType, str], ReceivePayloadType]
|
||||||
@ -122,7 +122,7 @@ class MQTTTagScanner(MqttDiscoveryDeviceUpdate):
|
|||||||
hass=self.hass,
|
hass=self.hass,
|
||||||
).async_render_with_possible_json_value
|
).async_render_with_possible_json_value
|
||||||
|
|
||||||
MqttDiscoveryDeviceUpdate.__init__(
|
MqttDiscoveryDeviceUpdateMixin.__init__(
|
||||||
self, hass, discovery_data, device_id, config_entry, LOG_NAME
|
self, hass, discovery_data, device_id, config_entry, LOG_NAME
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user