diff --git a/homeassistant/components/smartthings/__init__.py b/homeassistant/components/smartthings/__init__.py index 9bfa11d3293..bcc752ff173 100644 --- a/homeassistant/components/smartthings/__init__.py +++ b/homeassistant/components/smartthings/__init__.py @@ -11,7 +11,6 @@ import logging from aiohttp.client_exceptions import ClientConnectionError, ClientResponseError from pysmartapp.event import EVENT_TYPE_DEVICE from pysmartthings import Attribute, Capability, SmartThings -from pysmartthings.device import DeviceEntity from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_SECRET @@ -19,12 +18,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.typing import ConfigType from homeassistant.loader import async_get_loaded_integration @@ -433,42 +427,3 @@ class DeviceBroker: updated_devices.add(device.device_id) async_dispatcher_send(self._hass, SIGNAL_SMARTTHINGS_UPDATE, updated_devices) - - -class SmartThingsEntity(Entity): - """Defines a SmartThings entity.""" - - _attr_should_poll = False - - def __init__(self, device: DeviceEntity) -> None: - """Initialize the instance.""" - self._device = device - self._dispatcher_remove = None - self._attr_name = device.label - self._attr_unique_id = device.device_id - self._attr_device_info = DeviceInfo( - configuration_url="https://account.smartthings.com", - identifiers={(DOMAIN, device.device_id)}, - manufacturer=device.status.ocf_manufacturer_name, - model=device.status.ocf_model_number, - name=device.label, - hw_version=device.status.ocf_hardware_version, - sw_version=device.status.ocf_firmware_version, - ) - - async def async_added_to_hass(self): - """Device added to hass.""" - - async def async_update_state(devices): - """Update device state.""" - if self._device.device_id in devices: - await self.async_update_ha_state(True) - - self._dispatcher_remove = async_dispatcher_connect( - self.hass, SIGNAL_SMARTTHINGS_UPDATE, async_update_state - ) - - async def async_will_remove_from_hass(self) -> None: - """Disconnect the device when removed.""" - if self._dispatcher_remove: - self._dispatcher_remove() diff --git a/homeassistant/components/smartthings/binary_sensor.py b/homeassistant/components/smartthings/binary_sensor.py index 4bb60217eee..611473b011d 100644 --- a/homeassistant/components/smartthings/binary_sensor.py +++ b/homeassistant/components/smartthings/binary_sensor.py @@ -15,8 +15,8 @@ from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity CAPABILITY_TO_ATTRIB = { Capability.acceleration_sensor: Attribute.acceleration, diff --git a/homeassistant/components/smartthings/climate.py b/homeassistant/components/smartthings/climate.py index 0598e549f24..073a1470c21 100644 --- a/homeassistant/components/smartthings/climate.py +++ b/homeassistant/components/smartthings/climate.py @@ -28,8 +28,8 @@ from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity ATTR_OPERATION_STATE = "operation_state" MODE_TO_STATE = { diff --git a/homeassistant/components/smartthings/cover.py b/homeassistant/components/smartthings/cover.py index 276a68176b4..d0e2fc3f039 100644 --- a/homeassistant/components/smartthings/cover.py +++ b/homeassistant/components/smartthings/cover.py @@ -23,8 +23,8 @@ from homeassistant.const import ATTR_BATTERY_LEVEL from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity VALUE_TO_STATE = { "closed": STATE_CLOSED, diff --git a/homeassistant/components/smartthings/entity.py b/homeassistant/components/smartthings/entity.py new file mode 100644 index 00000000000..cc63213d122 --- /dev/null +++ b/homeassistant/components/smartthings/entity.py @@ -0,0 +1,50 @@ +"""Support for SmartThings Cloud.""" + +from __future__ import annotations + +from pysmartthings.device import DeviceEntity + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +from .const import DOMAIN, SIGNAL_SMARTTHINGS_UPDATE + + +class SmartThingsEntity(Entity): + """Defines a SmartThings entity.""" + + _attr_should_poll = False + + def __init__(self, device: DeviceEntity) -> None: + """Initialize the instance.""" + self._device = device + self._dispatcher_remove = None + self._attr_name = device.label + self._attr_unique_id = device.device_id + self._attr_device_info = DeviceInfo( + configuration_url="https://account.smartthings.com", + identifiers={(DOMAIN, device.device_id)}, + manufacturer=device.status.ocf_manufacturer_name, + model=device.status.ocf_model_number, + name=device.label, + hw_version=device.status.ocf_hardware_version, + sw_version=device.status.ocf_firmware_version, + ) + + async def async_added_to_hass(self): + """Device added to hass.""" + + async def async_update_state(devices): + """Update device state.""" + if self._device.device_id in devices: + await self.async_update_ha_state(True) + + self._dispatcher_remove = async_dispatcher_connect( + self.hass, SIGNAL_SMARTTHINGS_UPDATE, async_update_state + ) + + async def async_will_remove_from_hass(self) -> None: + """Disconnect the device when removed.""" + if self._dispatcher_remove: + self._dispatcher_remove() diff --git a/homeassistant/components/smartthings/fan.py b/homeassistant/components/smartthings/fan.py index 840c04c2a10..131cccdd869 100644 --- a/homeassistant/components/smartthings/fan.py +++ b/homeassistant/components/smartthings/fan.py @@ -18,8 +18,8 @@ from homeassistant.util.percentage import ( ) from homeassistant.util.scaling import int_states_in_range -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity SPEED_RANGE = (1, 3) # off is not included diff --git a/homeassistant/components/smartthings/light.py b/homeassistant/components/smartthings/light.py index 24a44a99d94..fd4b87f0ee7 100644 --- a/homeassistant/components/smartthings/light.py +++ b/homeassistant/components/smartthings/light.py @@ -23,8 +23,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.color as color_util -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity async def async_setup_entry( diff --git a/homeassistant/components/smartthings/lock.py b/homeassistant/components/smartthings/lock.py index 0cd954e7542..a0ae9e50443 100644 --- a/homeassistant/components/smartthings/lock.py +++ b/homeassistant/components/smartthings/lock.py @@ -12,8 +12,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity ST_STATE_LOCKED = "locked" ST_LOCK_ATTR_MAP = { diff --git a/homeassistant/components/smartthings/sensor.py b/homeassistant/components/smartthings/sensor.py index 2a61be3dc75..b73d3b43764 100644 --- a/homeassistant/components/smartthings/sensor.py +++ b/homeassistant/components/smartthings/sensor.py @@ -31,8 +31,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import dt as dt_util -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity class Map(NamedTuple): diff --git a/homeassistant/components/smartthings/switch.py b/homeassistant/components/smartthings/switch.py index bd5f7bc0b68..5cfe4576d6a 100644 --- a/homeassistant/components/smartthings/switch.py +++ b/homeassistant/components/smartthings/switch.py @@ -12,8 +12,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import SmartThingsEntity from .const import DATA_BROKERS, DOMAIN +from .entity import SmartThingsEntity async def async_setup_entry(