Reference no: EM133860445
Question
The SmartHome class must allow users to add devices to the collection, remove devices, retrieve specific devices, and toggle their on/off states. This class is responsible for controlling and interacting with all of the devices in the smart home system.
In the core task, focus on managing these devices within the SmartHome class and implementing the required functionality for adding, removing, retrieving, and toggling devices. Use the class diagram below as your starting point.
The SmartHome class must store a collection of SmartPlug objects and custom smart devices from Table 1. It should support multiple instances of each device type, allowing users to add various smart devices to the system.
The class must include an __init__() constructor to initialise the collection of devices. Users must be able to add new smart devices to the system using an add_device() method. To retrieve a specific device, the get_device(index) method should return the corresponding object from the collection. To control devices, they must be able to toggle a specific device's switched_on attribute by providing its position in the collection. Additionally, the SmartHome class should provide functionality to turn all devices on or off at once using the switch_all_on() and switch_all_off() methods.
When printed, the SmartHome should display a summary of the number of devices it contains, followed by details of each device. The __str__() method should generate an output in the following format:
SmartHome with 3 device(s):
1- SmartPlug is on with a consumption rate of 120
2- SmartOven is on with a temperature of 200
3- SmartPlug is off with a consumption rate of 90
Harder: Enhance the SmartHome class by introducing a max_items limit to restrict the number of devices that can be added to the collection. Once the limit is reached, any further attempts to add devices should be rejected with appropriate error handling.
Implement a remove_device(index) method that allows for the deletion of a device from the collection by specifying its position. If a valid index is provided, the device should be removed, and the list should update accordingly. If an invalid index is given, the method should raise an appropriate error.
Implement an update_option(index, value) method that allows modification of the option attribute of a device by specifying its position in the collection and a new option value. The attribute being updated will depend on the device type; for example, a SmartPlug will update its consumption_rate, while a SmartLight will change its brightness. Introduce error handling within the SmartHome class to ensure that all modifications remain within valid limits.
Testing
To evaluate the functionality of the SmartHome class, implement a test function called test_smart_home(). This function will systematically verify that devices can be added, toggled, updated, and that all constraints are properly enforced.
To begin testing the core functionality, create a SmartPlug and one of each of the two custom devices from Task 2. Initialise a SmartHome object and add all three devices to it. Print the state of the SmartHome to confirm that the devices have been correctly added.
Retrieve each device using the get_device(index) method and verify that the returned object matches the expected device at that position. Print the retrieved device details to confirm correctness.
Toggle each device individually using the toggle_device(index) method and print the SmartHome again to ensure that their switched_on attributes have been updated as expected.
Next, use the switch_all_on() method to turn on all devices at once, followed by switch_all_off() to turn them all off again. Print the final state of the SmartHome to verify that all devices have been switched off successfully and that their option attributes remain unchanged.
For the harder requirements, verify that the max_items limit is enforced by attempting to add more devices than allowed. The test should confirm that excess devices are rejected and that appropriate error handling is in place.
Modify the option attribute of both the SmartPlug and both the custom devices using update_option(index, value). First, assign a valid value from Table 1 and print the SmartHome to confirm that the change was successfully applied. Then, attempt to set the attribute to an invalid value and check that the update is rejected.
Test the remove_device(index: int) method to ensure that devices can be deleted correctly from SmartHome. First, delete a device from the collection and print the updated state of SmartHome to verify that it has been removed. Then, attempt to remove a device using an invalid index (e.g., a negative index or an index beyond the number of devices in the collection). The method should raise an appropriate error, preventing unintended deletions.
Test the update_option(index, value) to ensure that error handling is working as expected by trying to assign or update values outside the permitted range for each device's option attribute. If properly implemented, the SmartHome class should prevent these invalid updates, either by raising an exception or maintaining the current value.
Finally, print the SmartHome state one last time to verify that all changes, including toggling, option updates, and deletions, have been correctly applied and that invalid operations were successfully blocked.