diff --git a/homeassistant/components/sma/__init__.py b/homeassistant/components/sma/__init__.py index d8fcbbf8099..5db3039af40 100644 --- a/homeassistant/components/sma/__init__.py +++ b/homeassistant/components/sma/__init__.py @@ -32,6 +32,7 @@ from .const import ( DOMAIN, PLATFORMS, PYSMA_COORDINATOR, + PYSMA_DEVICE_INFO, PYSMA_OBJECT, PYSMA_REMOVE_LISTENER, PYSMA_SENSORS, @@ -141,6 +142,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: session = async_get_clientsession(hass, verify_ssl=verify_ssl) sma = pysma.SMA(session, url, password, group) + # Get updated device info + device_info = await sma.device_info() # Get all device sensors sensor_def = await sma.get_sensors() @@ -189,6 +192,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: PYSMA_COORDINATOR: coordinator, PYSMA_SENSORS: sensor_def, PYSMA_REMOVE_LISTENER: remove_stop_listener, + PYSMA_DEVICE_INFO: device_info, } hass.config_entries.async_setup_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/sma/config_flow.py b/homeassistant/components/sma/config_flow.py index a5147098c9f..66d875562ab 100644 --- a/homeassistant/components/sma/config_flow.py +++ b/homeassistant/components/sma/config_flow.py @@ -20,7 +20,7 @@ from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv -from .const import CONF_CUSTOM, CONF_GROUP, DEVICE_INFO, DOMAIN, GROUPS +from .const import CONF_CUSTOM, CONF_GROUP, DOMAIN, GROUPS _LOGGER = logging.getLogger(__name__) @@ -63,7 +63,6 @@ class SmaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): CONF_PASSWORD: vol.UNDEFINED, CONF_SENSORS: [], CONF_CUSTOM: {}, - DEVICE_INFO: {}, } async def async_step_user( @@ -79,7 +78,7 @@ class SmaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._data[CONF_PASSWORD] = user_input[CONF_PASSWORD] try: - self._data[DEVICE_INFO] = await validate_input(self.hass, user_input) + device_info = await validate_input(self.hass, user_input) except aiohttp.ClientError: errors["base"] = "cannot_connect" except InvalidAuth: @@ -91,7 +90,7 @@ class SmaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" if not errors: - await self.async_set_unique_id(self._data[DEVICE_INFO]["serial"]) + await self.async_set_unique_id(device_info["serial"]) self._abort_if_unique_id_configured() return self.async_create_entry( title=self._data[CONF_HOST], data=self._data @@ -120,7 +119,6 @@ class SmaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) -> FlowResult: """Import a config flow from configuration.""" device_info = await validate_input(self.hass, import_config) - import_config[DEVICE_INFO] = device_info # If unique is configured import was already run # This means remap was already done, so we can abort diff --git a/homeassistant/components/sma/const.py b/homeassistant/components/sma/const.py index 2e1086e48a2..91173d95493 100644 --- a/homeassistant/components/sma/const.py +++ b/homeassistant/components/sma/const.py @@ -6,6 +6,7 @@ PYSMA_COORDINATOR = "coordinator" PYSMA_OBJECT = "pysma" PYSMA_REMOVE_LISTENER = "remove_listener" PYSMA_SENSORS = "pysma_sensors" +PYSMA_DEVICE_INFO = "device_info" PLATFORMS = ["sensor"] @@ -14,7 +15,6 @@ CONF_FACTOR = "factor" CONF_GROUP = "group" CONF_KEY = "key" CONF_UNIT = "unit" -DEVICE_INFO = "device_info" DEFAULT_SCAN_INTERVAL = 5 diff --git a/homeassistant/components/sma/sensor.py b/homeassistant/components/sma/sensor.py index 2ef999a6579..3894f864ffb 100644 --- a/homeassistant/components/sma/sensor.py +++ b/homeassistant/components/sma/sensor.py @@ -33,10 +33,10 @@ from .const import ( CONF_GROUP, CONF_KEY, CONF_UNIT, - DEVICE_INFO, DOMAIN, GROUPS, PYSMA_COORDINATOR, + PYSMA_DEVICE_INFO, PYSMA_SENSORS, ) @@ -124,6 +124,7 @@ async def async_setup_entry( coordinator = sma_data[PYSMA_COORDINATOR] used_sensors = sma_data[PYSMA_SENSORS] + device_info = sma_data[PYSMA_DEVICE_INFO] entities = [] for sensor in used_sensors: @@ -131,7 +132,7 @@ async def async_setup_entry( SMAsensor( coordinator, config_entry.unique_id, - config_entry.data[DEVICE_INFO], + device_info, sensor, ) ) @@ -185,11 +186,15 @@ class SMAsensor(CoordinatorEntity, SensorEntity): @property def device_info(self) -> DeviceInfo: """Return the device information.""" + if not self._device_info: + return None + return { "identifiers": {(DOMAIN, self._config_entry_unique_id)}, "name": self._device_info["name"], "manufacturer": self._device_info["manufacturer"], "model": self._device_info["type"], + "sw_version": self._device_info["sw_version"], } @property diff --git a/tests/components/sma/__init__.py b/tests/components/sma/__init__.py index 0797558958e..4210772420c 100644 --- a/tests/components/sma/__init__.py +++ b/tests/components/sma/__init__.py @@ -69,7 +69,6 @@ MOCK_CUSTOM_SENSOR2 = { MOCK_SETUP_DATA = dict( { "custom": {}, - "device_info": MOCK_DEVICE, "sensors": [], }, **MOCK_USER_INPUT, @@ -91,7 +90,6 @@ MOCK_CUSTOM_SETUP_DATA = dict( "unit": MOCK_CUSTOM_SENSOR2["unit"], }, }, - "device_info": MOCK_DEVICE, "sensors": [], }, **MOCK_USER_INPUT,