Skip to content

Commit

Permalink
Add reboot service
Browse files Browse the repository at this point in the history
  • Loading branch information
roleoroleo committed Feb 1, 2025
1 parent 926f106 commit a6f54a6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
29 changes: 27 additions & 2 deletions custom_components/yi_hack/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
CONF_MQTT_PREFIX, CONF_PTZ,
CONF_TOPIC_MOTION_DETECTION_IMAGE, DEFAULT_BRAND, DOMAIN,
HTTP_TIMEOUT, LINK_HIGH_RES_STREAM, LINK_LOW_RES_STREAM,
MSTAR, SERVICE_PTZ, SERVICE_SPEAK)
MSTAR, SERVICE_MOVE_TO_PRESET, SERVICE_PTZ,
SERVICE_REBOOT, SERVICE_SPEAK)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +72,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_
)

platform.async_register_entity_service(
"move_to_preset",
SERVICE_MOVE_TO_PRESET,
{
vol.Required("preset_id"): vol.All(int, vol.Range(min=0, max=14)),
},
Expand All @@ -97,6 +98,12 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_
"async_perform_speak",
)

platform.async_register_entity_service(
SERVICE_REBOOT,
{},
"async_perform_reboot",
)

async_add_entities(
[
YiHackCamera(hass, config),
Expand Down Expand Up @@ -383,6 +390,24 @@ async def async_perform_speak(self, language, sentence):

await self.hass.async_add_executor_job(self._perform_speak, language, sentence)

def _perform_reboot(self):
auth = None
if self._user or self._password:
auth = HTTPBasicAuth(self._user, self._password)

try:
response = requests.get(f"http://{self._host}:{self._port}/cgi-bin/reboot.sh", timeout=HTTP_TIMEOUT, auth=auth)
if response.status_code >= 300:
_LOGGER.error(f"Failed to send reboot command to device {self._host}")
except requests.exceptions.RequestException as error:
_LOGGER.error(f"Failed to send reboot command to device {self._host}: error {error}")

async def async_perform_reboot(self):
"""Reboot the camera."""
_LOGGER.debug(f"Reboot the camera")

await self.hass.async_add_executor_job(self._perform_reboot)

@property
def brand(self):
"""Camera brand."""
Expand Down
2 changes: 2 additions & 0 deletions custom_components/yi_hack/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
DEFAULT_EXTRA_ARGUMENTS = "-rtsp_transport tcp"

SERVICE_PTZ = "ptz"
SERVICE_MOVE_TO_PRESET = "move_to_preset"
SERVICE_SPEAK = "speak"
SERVICE_REBOOT = "reboot"

HTTP_TIMEOUT = 10

Expand Down
3 changes: 2 additions & 1 deletion custom_components/yi_hack/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"services": {
"ptz": "mdi:camera-control",
"move_to_preset": "mdi:image-outline",
"speak": "mdi:speaker-message"
"speak": "mdi:speaker-message",
"reboot": "mdi:restart"
}
}
2 changes: 1 addition & 1 deletion custom_components/yi_hack/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"documentation": "https://github.com/roleoroleo/yi-hack_ha_integration",
"iot_class": "local_push",
"issue_tracker": "https://github.com/roleoroleo/yi-hack_ha_integration/issues",
"version": "0.5.2"
"version": "0.5.3"
}
14 changes: 14 additions & 0 deletions custom_components/yi_hack/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ speak:
example: "Hello world!"
selector:
text:

reboot:
name: Reboot the cam
description: Reboot the camera
fields:
entity_id:
name: Entity id
description: Name of entity to reboot.
required: true
example: "camera.living_room_camera"
selector:
entity:
integration: yi_hack
domain: camera

0 comments on commit a6f54a6

Please sign in to comment.