Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion homeassistant/components/shelly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, device_registry, update_coordinator
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -71,6 +71,8 @@
)
CONFIG_SCHEMA: Final = vol.Schema({DOMAIN: COAP_SCHEMA}, extra=vol.ALLOW_EXTRA)

SERVICE_UPDATE_FIRMWARE = "update_firmware"


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Shelly component."""
Expand Down Expand Up @@ -103,6 +105,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if get_device_entry_gen(entry) == 2:
return await async_setup_rpc_entry(hass, entry)

await async_setup_services(hass)

return await async_setup_block_entry(hass, entry)


Expand Down Expand Up @@ -222,6 +226,25 @@ async def async_setup_rpc_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool
return True


async def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services."""

if hass.services.has_service(DOMAIN, SERVICE_UPDATE_FIRMWARE):
return

async def async_update_firmware(call: ServiceCall) -> None:
"""Trigger an OTA update of the device firmware."""
device_id = call.data["device_id"]
beta = call.data["beta"]

device_wrapper = get_block_device_wrapper(hass, device_id)
if device_wrapper is not None:
result = await device_wrapper.device.trigger_ota_update(beta)
_LOGGER.debug("Firmware update response: %s", result)

hass.services.async_register(DOMAIN, SERVICE_UPDATE_FIRMWARE, async_update_firmware)


class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator):
"""Wrapper for a Shelly block based device with Home Assistant specific functions."""

Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/shelly/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
update_firmware:
name: Update firmware
description: Updates the Shelly device firmware.
fields:
device_id:
name: Device
description: The Shelly device to update
required: true
selector:
device:
integration: shelly
beta:
name: Beta
description: Whether to install a beta version of the firmware
required: true
example: true
advanced: true
default: false
selector:
boolean: