-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add switches and sensors to Litter-Robot #46942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b5446c
Add switch es and sensors, as well as associated tests, to Litter-Robot
natekspencer 6575505
Create class for each switch since they don't share functionality
natekspencer 7266af6
Remove night mode switch and move attributes to vacuum entity
natekspencer fb51a70
Rename Night Light to Night Light Mode for clarity
natekspencer d5f32b3
Update tests to comply with guidelines
natekspencer b0c79c8
Adjust patch on tests
natekspencer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Support for Litter-Robot sensors.""" | ||
| from homeassistant.const import PERCENTAGE | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| from .const import DOMAIN | ||
| from .hub import LitterRobotEntity | ||
|
|
||
| WASTE_DRAWER = "Waste Drawer" | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up Litter-Robot sensors using config entry.""" | ||
| hub = hass.data[DOMAIN][config_entry.entry_id] | ||
|
|
||
| entities = [] | ||
| for robot in hub.account.robots: | ||
| entities.append(LitterRobotSensor(robot, WASTE_DRAWER, hub)) | ||
|
|
||
| if entities: | ||
| async_add_entities(entities, True) | ||
|
|
||
|
|
||
| class LitterRobotSensor(LitterRobotEntity, Entity): | ||
| """Litter-Robot sensors.""" | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state.""" | ||
| return self.robot.waste_drawer_gauge | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return unit of measurement.""" | ||
| return PERCENTAGE | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon to use in the frontend, if any.""" | ||
| if self.robot.waste_drawer_gauge <= 10: | ||
| return "mdi:gauge-empty" | ||
| if self.robot.waste_drawer_gauge < 50: | ||
| return "mdi:gauge-low" | ||
| if self.robot.waste_drawer_gauge <= 90: | ||
| return "mdi:gauge" | ||
| return "mdi:gauge-full" | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
| """Return device specific state attributes.""" | ||
| return { | ||
| "cycle_count": self.robot.cycle_count, | ||
| "cycle_capacity": self.robot.cycle_capacity, | ||
| "cycles_after_drawer_full": self.robot.cycles_after_drawer_full, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """Support for Litter-Robot switches.""" | ||
| from homeassistant.helpers.entity import ToggleEntity | ||
|
|
||
| from .const import DOMAIN | ||
| from .hub import LitterRobotEntity | ||
|
|
||
|
|
||
| class LitterRobotNightLightModeSwitch(LitterRobotEntity, ToggleEntity): | ||
|
natekspencer marked this conversation as resolved.
|
||
| """Litter-Robot Night Light Mode Switch.""" | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if switch is on.""" | ||
| return self.robot.night_light_active | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon.""" | ||
| return "mdi:lightbulb-on" if self.is_on else "mdi:lightbulb-off" | ||
|
|
||
| async def async_turn_on(self, **kwargs): | ||
| """Turn the switch on.""" | ||
| await self.perform_action_and_refresh(self.robot.set_night_light, True) | ||
|
|
||
| async def async_turn_off(self, **kwargs): | ||
| """Turn the switch off.""" | ||
| await self.perform_action_and_refresh(self.robot.set_night_light, False) | ||
|
|
||
|
|
||
| class LitterRobotPanelLockoutSwitch(LitterRobotEntity, ToggleEntity): | ||
| """Litter-Robot Panel Lockout Switch.""" | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if switch is on.""" | ||
| return self.robot.panel_lock_active | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon.""" | ||
| return "mdi:lock" if self.is_on else "mdi:lock-open" | ||
|
|
||
| async def async_turn_on(self, **kwargs): | ||
| """Turn the switch on.""" | ||
| await self.perform_action_and_refresh(self.robot.set_panel_lockout, True) | ||
|
|
||
| async def async_turn_off(self, **kwargs): | ||
| """Turn the switch off.""" | ||
| await self.perform_action_and_refresh(self.robot.set_panel_lockout, False) | ||
|
|
||
|
|
||
| ROBOT_SWITCHES = { | ||
| "Night Light Mode": LitterRobotNightLightModeSwitch, | ||
| "Panel Lockout": LitterRobotPanelLockoutSwitch, | ||
| } | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up Litter-Robot switches using config entry.""" | ||
| hub = hass.data[DOMAIN][config_entry.entry_id] | ||
|
|
||
| entities = [] | ||
| for robot in hub.account.robots: | ||
| for switch_type, switch_class in ROBOT_SWITCHES.items(): | ||
| entities.append(switch_class(robot, switch_type, hub)) | ||
|
|
||
| if entities: | ||
| async_add_entities(entities, True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| VacuumEntity, | ||
| ) | ||
| from homeassistant.const import STATE_OFF | ||
| import homeassistant.util.dt as dt_util | ||
|
|
||
| from .const import DOMAIN | ||
| from .hub import LitterRobotEntity | ||
|
|
@@ -118,9 +119,21 @@ async def async_send_command(self, command, params=None, **kwargs): | |
| @property | ||
| def device_state_attributes(self): | ||
| """Return device specific state attributes.""" | ||
| [sleep_mode_start_time, sleep_mode_end_time] = [None, None] | ||
|
|
||
| if self.robot.sleep_mode_active: | ||
| sleep_mode_start_time = dt_util.as_local( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Times in state attributes must be absolute UTC time. |
||
| self.robot.sleep_mode_start_time | ||
| ).strftime("%H:%M:00") | ||
| sleep_mode_end_time = dt_util.as_local( | ||
| self.robot.sleep_mode_end_time | ||
| ).strftime("%H:%M:00") | ||
|
|
||
| return { | ||
| "clean_cycle_wait_time_minutes": self.robot.clean_cycle_wait_time_minutes, | ||
| "is_sleeping": self.robot.is_sleeping, | ||
| "sleep_mode_start_time": sleep_mode_start_time, | ||
| "sleep_mode_end_time": sleep_mode_end_time, | ||
| "power_status": self.robot.power_status, | ||
| "unit_status_code": self.robot.unit_status.name, | ||
| "last_seen": self.robot.last_seen, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """Test the Litter-Robot sensor entity.""" | ||
| from homeassistant.components.sensor import DOMAIN as PLATFORM_DOMAIN | ||
| from homeassistant.const import PERCENTAGE | ||
|
|
||
| from .conftest import setup_hub | ||
|
|
||
| ENTITY_ID = "sensor.test_waste_drawer" | ||
|
|
||
|
|
||
| async def test_sensor(hass, mock_hub): | ||
| """Tests the sensor entity was set up.""" | ||
| await setup_hub(hass, mock_hub, PLATFORM_DOMAIN) | ||
|
|
||
| sensor = hass.states.get(ENTITY_ID) | ||
| assert sensor | ||
| assert sensor.state == "50" | ||
| assert sensor.attributes["cycle_count"] == 15 | ||
| assert sensor.attributes["cycle_capacity"] == 30 | ||
| assert sensor.attributes["cycles_after_drawer_full"] == 0 | ||
| assert sensor.attributes["unit_of_measurement"] == PERCENTAGE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Test the Litter-Robot switch entity.""" | ||
| from datetime import timedelta | ||
|
|
||
| import pytest | ||
|
|
||
| from homeassistant.components.litterrobot.hub import REFRESH_WAIT_TIME | ||
| from homeassistant.components.switch import ( | ||
| DOMAIN as PLATFORM_DOMAIN, | ||
| SERVICE_TURN_OFF, | ||
| SERVICE_TURN_ON, | ||
| ) | ||
| from homeassistant.const import ATTR_ENTITY_ID, STATE_ON | ||
| from homeassistant.util.dt import utcnow | ||
|
|
||
| from .conftest import setup_hub | ||
|
|
||
| from tests.common import async_fire_time_changed | ||
|
|
||
| NIGHT_LIGHT_MODE_ENTITY_ID = "switch.test_night_light_mode" | ||
| PANEL_LOCKOUT_ENTITY_ID = "switch.test_panel_lockout" | ||
|
|
||
|
|
||
| async def test_switch(hass, mock_hub): | ||
| """Tests the switch entity was set up.""" | ||
| await setup_hub(hass, mock_hub, PLATFORM_DOMAIN) | ||
|
|
||
| switch = hass.states.get(NIGHT_LIGHT_MODE_ENTITY_ID) | ||
| assert switch | ||
| assert switch.state == STATE_ON | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "entity_id,robot_command", | ||
| [ | ||
| (NIGHT_LIGHT_MODE_ENTITY_ID, "set_night_light"), | ||
| (PANEL_LOCKOUT_ENTITY_ID, "set_panel_lockout"), | ||
| ], | ||
| ) | ||
| async def test_on_off_commands(hass, mock_hub, entity_id, robot_command): | ||
| """Test sending commands to the switch.""" | ||
| await setup_hub(hass, mock_hub, PLATFORM_DOMAIN) | ||
|
|
||
| switch = hass.states.get(entity_id) | ||
| assert switch | ||
|
|
||
| data = {ATTR_ENTITY_ID: entity_id} | ||
|
|
||
| count = 0 | ||
| for service in [SERVICE_TURN_ON, SERVICE_TURN_OFF]: | ||
| count += 1 | ||
| await hass.services.async_call( | ||
| PLATFORM_DOMAIN, | ||
| service, | ||
| data, | ||
| blocking=True, | ||
| ) | ||
| future = utcnow() + timedelta(seconds=REFRESH_WAIT_TIME) | ||
| async_fire_time_changed(hass, future) | ||
| assert getattr(mock_hub.account.robots[0], robot_command).call_count == count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these attributes be separate sensor entities instead? If a measurement is relevant on its own we want it to be a separate entity.