Skip to content

Commit

Permalink
Fix services schema and improve selectors (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Nov 13, 2023
1 parent 0cef1c4 commit cfc069c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
47 changes: 24 additions & 23 deletions custom_components/google_home/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity import Entity, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -82,27 +82,26 @@ async def async_setup_entry(
]
async_add_devices(sensors)

platform = entity_platform.current_platform.get()
platform = entity_platform.async_get_current_platform()

# Services
if platform is not None:
platform.async_register_entity_service(
SERVICE_DELETE_ALARM,
vol.Schema({vol.Required(SERVICE_ATTR_ALARM_ID): cv.string}),
GoogleHomeAlarmsSensor.async_delete_alarm.__name__,
)

platform.async_register_entity_service(
SERVICE_DELETE_TIMER,
vol.Schema({vol.Required(SERVICE_ATTR_TIMER_ID): cv.string}),
GoogleHomeTimersSensor.async_delete_timer.__name__,
)

platform.async_register_entity_service(
SERVICE_REBOOT,
vol.Schema({}),
GoogleHomeDeviceSensor.async_reboot_device.__name__,
)
platform.async_register_entity_service(
SERVICE_DELETE_ALARM,
{vol.Required(SERVICE_ATTR_ALARM_ID): cv.string}, # type: ignore[dict-item]
GoogleHomeAlarmsSensor.async_delete_alarm,
)

platform.async_register_entity_service(
SERVICE_DELETE_TIMER,
{vol.Required(SERVICE_ATTR_TIMER_ID): cv.string}, # type: ignore[dict-item]
GoogleHomeTimersSensor.async_delete_timer,
)

platform.async_register_entity_service(
SERVICE_REBOOT,
{},
GoogleHomeDeviceSensor.async_reboot_device,
)

return True

Expand Down Expand Up @@ -147,7 +146,7 @@ def get_device_attributes(device: GoogleHomeDevice) -> DeviceAttributes:
"available": device.available,
}

async def async_reboot_device(self) -> None:
async def async_reboot_device(self, _call: ServiceCall) -> None:
"""Reboot the device."""
device = self.get_device()

Expand Down Expand Up @@ -222,14 +221,15 @@ def is_valid_alarm_id(alarm_id: str) -> bool:
alarm_id.startswith("alarm/") and len(alarm_id) == ALARM_AND_TIMER_ID_LENGTH
)

async def async_delete_alarm(self, alarm_id: str) -> None:
async def async_delete_alarm(self, call: ServiceCall) -> None:
"""Service call to delete alarm on device"""
device = self.get_device()

if device is None:
_LOGGER.error("Device %s is not found.", self.device_name)
return

alarm_id: str = call.data[SERVICE_ATTR_ALARM_ID]
if not self.is_valid_alarm_id(alarm_id):
_LOGGER.error(
"Incorrect ID format! Please provide a valid alarm ID. "
Expand Down Expand Up @@ -295,14 +295,15 @@ def is_valid_timer_id(timer_id: str) -> bool:
timer_id.startswith("timer/") and len(timer_id) == ALARM_AND_TIMER_ID_LENGTH
)

async def async_delete_timer(self, timer_id: str) -> None:
async def async_delete_timer(self, call: ServiceCall) -> None:
"""Service call to delete alarm on device"""
device = self.get_device()

if device is None:
_LOGGER.error("Device %s is not found.", self.device_name)
return

timer_id: str = call.data[SERVICE_ATTR_TIMER_ID]
if not self.is_valid_timer_id(timer_id):
_LOGGER.error(
"Incorrect ID format! Please provide a valid timer ID. "
Expand Down
20 changes: 10 additions & 10 deletions custom_components/google_home/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
reboot_device:
name: Reboot device
description: Reboot a Google Home device.
fields:
entity_id:
name: Entity
description: Represents a Google Home device (sensor.xxxx_device).
example: "sensor.kitchen_device"
required: true
selector:
text:
target:
entity:
domain: sensor
integration: google_home

delete_alarm:
name: Delete alarm
Expand All @@ -22,7 +18,9 @@ delete_alarm:
example: "sensor.kitchen_alarms"
required: true
selector:
text:
entity:
domain: sensor
integration: google_home
alarm_id:
name: Alarm ID
description: ID of an alarm (alarm/xxx).
Expand All @@ -41,7 +39,9 @@ delete_timer:
example: "sensor.kitchen_timers"
required: true
selector:
text:
entity:
domain: sensor
integration: google_home
timer_id:
name: Timer ID
description: ID of a timer (timer/xxx).
Expand Down

0 comments on commit cfc069c

Please sign in to comment.