Skip to content

Commit

Permalink
Store runtime data inside the config entry in Tuya (home-assistant#11…
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 authored May 8, 2024
1 parent 89049bc commit 12759b5
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 92 deletions.
17 changes: 8 additions & 9 deletions homeassistant/components/tuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
# Suppress logs from the library, it logs unneeded on error
logging.getLogger("tuya_sharing").setLevel(logging.CRITICAL)

TuyaConfigEntry = ConfigEntry["HomeAssistantTuyaData"]


class HomeAssistantTuyaData(NamedTuple):
"""Tuya data stored in the Home Assistant data object."""
Expand All @@ -43,7 +45,7 @@ class HomeAssistantTuyaData(NamedTuple):
listener: SharingDeviceListener


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: TuyaConfigEntry) -> bool:
"""Async setup hass config entry."""
if CONF_APP_TYPE in entry.data:
raise ConfigEntryAuthFailed("Authentication failed. Please re-authenticate.")
Expand Down Expand Up @@ -73,9 +75,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
raise

# Connection is successful, store the manager & listener
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantTuyaData(
manager=manager, listener=listener
)
entry.runtime_data = HomeAssistantTuyaData(manager=manager, listener=listener)

# Cleanup device registry
await cleanup_device_registry(hass, manager)
Expand Down Expand Up @@ -108,18 +108,17 @@ async def cleanup_device_registry(hass: HomeAssistant, device_manager: Manager)
break


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: TuyaConfigEntry) -> bool:
"""Unloading the Tuya platforms."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
tuya: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
tuya = entry.runtime_data
if tuya.manager.mq is not None:
tuya.manager.mq.stop()
tuya.manager.remove_device_listener(tuya.listener)
del hass.data[DOMAIN][entry.entry_id]
return unload_ok


async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def async_remove_entry(hass: HomeAssistant, entry: TuyaConfigEntry) -> None:
"""Remove a config entry.
This will revoke the credentials from Tuya.
Expand Down Expand Up @@ -184,7 +183,7 @@ class TokenListener(SharingTokenListener):
def __init__(
self,
hass: HomeAssistant,
entry: ConfigEntry,
entry: TuyaConfigEntry,
) -> None:
"""Init TokenListener."""
self.hass = hass
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
AlarmControlPanelEntityDescription,
AlarmControlPanelEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME,
Expand All @@ -22,9 +21,9 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType


class Mode(StrEnum):
Expand Down Expand Up @@ -59,10 +58,10 @@ class Mode(StrEnum):


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya alarm dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
from .const import TUYA_DISCOVERY_NEW, DPCode


@dataclass(frozen=True)
Expand Down Expand Up @@ -338,10 +337,10 @@ class TuyaBinarySensorEntityDescription(BinarySensorEntityDescription):


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya binary sensor dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from tuya_sharing import CustomerDevice, Manager

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
from .const import TUYA_DISCOVERY_NEW, DPCode

# All descriptions can be found here.
# https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
Expand Down Expand Up @@ -59,10 +58,10 @@


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya buttons dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

from homeassistant.components import ffmpeg
from homeassistant.components.camera import Camera as CameraEntity, CameraEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
from .const import TUYA_DISCOVERY_NEW, DPCode

# All descriptions can be found here:
# https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
Expand All @@ -25,10 +24,10 @@


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya cameras dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
ClimateEntityFeature,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType

TUYA_HVAC_TO_HA = {
"auto": HVACMode.HEAT_COOL,
Expand Down Expand Up @@ -82,10 +81,10 @@ class TuyaClimateEntityDescription(ClimateEntityDescription):


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya climate dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
CoverEntityDescription,
CoverEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType


@dataclass(frozen=True)
Expand Down Expand Up @@ -143,10 +142,10 @@ class TuyaCoverEntityDescription(CoverEntityDescription):


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya cover dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
11 changes: 5 additions & 6 deletions homeassistant/components/tuya/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
from tuya_sharing import CustomerDevice

from homeassistant.components.diagnostics import REDACTED
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.util import dt as dt_util

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .const import DOMAIN, DPCode


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: TuyaConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
return _async_get_diagnostics(hass, entry)


async def async_get_device_diagnostics(
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
hass: HomeAssistant, entry: TuyaConfigEntry, device: DeviceEntry
) -> dict[str, Any]:
"""Return diagnostics for a device entry."""
return _async_get_diagnostics(hass, entry, device)
Expand All @@ -36,11 +35,11 @@ async def async_get_device_diagnostics(
@callback
def _async_get_diagnostics(
hass: HomeAssistant,
entry: ConfigEntry,
entry: TuyaConfigEntry,
device: DeviceEntry | None = None,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

mqtt_connected = None
if hass_data.manager.mq.client:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
FanEntity,
FanEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -21,9 +20,9 @@
percentage_to_ordered_list_item,
)

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import EnumTypeData, IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType

TUYA_SUPPORT_TYPE = {
"fs", # Fan
Expand All @@ -35,10 +34,10 @@


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up tuya fan dynamically through tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
HumidifierEntityDescription,
HumidifierEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType


@dataclass(frozen=True)
Expand Down Expand Up @@ -56,10 +55,10 @@ class TuyaHumidifierEntityDescription(HumidifierEntityDescription):


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya (de)humidifier dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]) -> None:
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/tuya/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
LightEntityDescription,
filter_supported_color_modes,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantTuyaData
from . import TuyaConfigEntry
from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType, WorkMode
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType, WorkMode
from .util import remap_value


Expand Down Expand Up @@ -409,10 +408,10 @@ def brightness(self) -> int:


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up tuya light dynamically through tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
hass_data = entry.runtime_data

@callback
def async_discover_device(device_ids: list[str]):
Expand Down
Loading

0 comments on commit 12759b5

Please sign in to comment.